blob: 96418b20af2b9717de961531e22281e2675b37bc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
package com.javispedro.autobluetether;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
/**
* A fragment representing a list of Items.
*/
public class DeviceFragment extends Fragment {
private final static String TAG = "DeviceFragment";
private RecyclerView mRecyclerView;
private BtDeviceRecyclerViewAdapter mAdapter;
private BtDeviceEnabledSetAdapter mEnabledAdapter;
private static final String ARG_COLUMN_COUNT = "column-count";
private int mColumnCount = 1;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public DeviceFragment() {
}
@SuppressWarnings("unused")
public static DeviceFragment newInstance(int columnCount) {
DeviceFragment fragment = new DeviceFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_device_list, container, false);
mRecyclerView = (RecyclerView) view;
Context context = view.getContext();
if (mColumnCount <= 1) {
mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
mRecyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
mEnabledAdapter = new BtDeviceEnabledSetAdapter(prefs, getString(R.string.prefs_key_device_list));
mAdapter = new BtDeviceRecyclerViewAdapter();
mAdapter.setBtDeviceEnabledSetAdapter(mEnabledAdapter);
mRecyclerView.setAdapter(mAdapter);
return mRecyclerView;
}
@Override
public void onDestroyView() {
super.onDestroyView();
mRecyclerView = null;
mAdapter = null;
mEnabledAdapter = null;
}
public void refreshList() {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null) {
Log.e(TAG, "no bluetooth adapter found?");
Toast.makeText(getContext(), getString(R.string.no_bluetooth_adapter), Toast.LENGTH_LONG).show();
return;
}
ArrayList<BluetoothDevice> devices = new ArrayList<>(adapter.getBondedDevices());
mAdapter.setValues(devices);
}
}
|