SetDeviceActivity.java
6.03 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.emmoco.example.blinker;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.emmoco.android.TargetConnection;
import java.util.ArrayList;
/**
* This activity manages a screen where the user can choose from a list of target devices. These devices include
* the Bluetooth paired devices and the Emmoco mock devices used for testing.
*
* This activity maintains a simple listview. There is an option to jump to the Settings->Bluetooth-Scan activity
* When this activity returns, it refreshes the paired bluetooth device list in case it has changed
*
* This uses a custom Listview Adapter to show a checkmark next to the currently saved device
*
* The devices is stored using standard Android SharedPreferences
*
*/
public class SetDeviceActivity extends Activity{
final static private String TAG="SetDeviceActivity";
private String mCurrentDevice;
private ListView mDeviceList;
private DeviceArrayAdapter mAdapter;
final public static String PREF_DEVICE_NAME = "DEVICE";
final public static String MOCK_DEVICE_LABEL = "Mock Blinker Device";
/**
* Internal class that adapts our array of device strings to a listview. Current choice is highlighted with
* a checkmark
*/
private class DeviceArrayAdapter extends BaseAdapter {
ArrayList<String> mList;
Activity mContext;
DeviceArrayAdapter(Activity context, ArrayList<String> arrayList) {
mList = arrayList;
mContext = context;
}
public int getCount() {
return mList.size();
}
public Object getItem(int i) {
return mList.get(i);
}
public long getItemId(int i) {
return i;
}
/**
* Returns a new view constructed from a simple layout. The layout has a textview for the name of the device
* and a checkmark graphic that is visible or not.
*
* @param i
* @param view
* @param viewGroup
* @return
*/
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflator = mContext.getLayoutInflater();
view = inflator.inflate(R.layout.list_item, null);
String name = (String)getItem(i);
String label = name;
// Translate the URI for the device into a pretty label for display in the listview
// If Mock device, use mock label
if (name.contentEquals(TargetConnection.MOCK_BLINKER_PRE)){
label = MOCK_DEVICE_LABEL;
}
if (name.startsWith(TargetConnection.BT_PRE)) {
// If Bluetooth, remove prefix
label = name.substring(TargetConnection.BT_PRE.length());
}
// Set the label in the view
((TextView)view.findViewById(R.id.item_label)).setText(label);
// If the current one, show the checkbox
if (mList.get(i).contentEquals(mCurrentDevice)){
view.findViewById(R.id.checkmark).setVisibility(View.VISIBLE);
}else{
view.findViewById(R.id.checkmark).setVisibility(View.INVISIBLE);
}
return view;
}
// Replace the list with a new one and trigger a redraw on the screen
public void updateList(ArrayList<String> list){
mList = list;
notifyDataSetInvalidated();
}
}
/**
* Standard Android method called at start of activity.
*
* Save the
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.set_device_activity);
mDeviceList = (ListView)findViewById(R.id.device_list_view);
mAdapter = new DeviceArrayAdapter(this,TargetConnection.deviceList(true,false));
mDeviceList.setAdapter(mAdapter);
// get the device to connect to from the users preferences
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
mCurrentDevice = prefs.getString(PREF_DEVICE_NAME, TargetConnection.MOCK_BLINKER_PRE);
// Register a handler when one of the list items is clicked. This handler will set the
// preference to use the new device, and finish this activity so we go back to the main activity
mDeviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String newDeviceName = (String)mAdapter.getItem(i);
// Save changed name to preferences
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SetDeviceActivity.this);
SharedPreferences.Editor edit = prefs.edit();
edit.putString(PREF_DEVICE_NAME, newDeviceName);
edit.commit();
// Set the result so the calling activity know we have changed the device
setResult(RESULT_OK);
finish();
}
} );
}
@Override
protected void onResume() {
// If we are resuming after going to Settings-->Bluetooth, the device list may have changed so update
mAdapter.updateList(TargetConnection.deviceList(true,false));
super.onResume();
}
/**
* Use an intent to call the Android bluetooth settings for scanning bluetooth devices
* @param v
*/
public void onScanDeviceClick(View v) {
Intent i = new Intent();
i.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
startActivity(i);
}
}