-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathPickFragment.java
executable file
·436 lines (362 loc) · 11.3 KB
/
PickFragment.java
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package com.enterpriseandroidbook.contactscontractexample;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ListIterator;
import android.app.Activity;
import android.app.ListFragment;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.content.res.Configuration;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
/**
* @author default-name
*
*/
/**
* @author default-name
*
*/
public class PickFragment extends ListFragment implements
LoaderManager.LoaderCallbacks<Cursor>, OnMenuItemClickListener {
// Turn logging on or off
private static final boolean L = true;
// String for logging the class name
private final String CLASSNAME = getClass().getSimpleName();
// Tag my loader with this ID
public static final int LOADER_ID = 42;
// Labels for members saved as state
private final String STATE_LABEL_NAME = "tablename";
// The current table's class name
private String tableName;
public void onAttach(Activity activity) {
super.onAttach(activity);
// Notification that the fragment is associated with an Activity
if (L)
Log.i(CLASSNAME, "onAttach " + activity.getClass().getSimpleName());
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Tell the system we have an options menu
setHasOptionsMenu(true);
doLoaderCreation(savedInstanceState);
// Notification that
if (L)
Log.i(CLASSNAME, "onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
final LinearLayout listLayout = (LinearLayout) inflater.inflate(
R.layout.list_frag_list, container, false);
if (L)
Log.i(CLASSNAME, "onCreateView");
return listLayout;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(STATE_LABEL_NAME, tableName);
}
public void onStart() {
super.onStart();
if (L)
Log.i(CLASSNAME, "onStart");
}
public void onResume() {
super.onResume();
if (L)
Log.i(CLASSNAME, "onResume");
}
public void onPause() {
super.onPause();
if (L)
Log.i(CLASSNAME, "onPause");
}
public void onStop() {
super.onStop();
if (L)
Log.i(CLASSNAME, "onStop");
}
public void onDestroyView() {
super.onDestroyView();
if (L)
Log.i(CLASSNAME, "onDestroyView");
}
public void onDestroy() {
super.onDestroy();
if (L)
Log.i(CLASSNAME, "onDestroy");
}
public void onDetach() {
super.onDetach();
if (L)
Log.i(CLASSNAME, "onDetach");
}
// ////////////////////////////////////////////////////////////////////////////
// Minor lifecycle methods
// ////////////////////////////////////////////////////////////////////////////
public void onActivityCreated() {
// Notification that the containing activity and its View hierarchy exist
if (L)
Log.i(CLASSNAME, "onActivityCreated");
}
// /////////////////////////////////////////////////////////////////////////////
// Overrides of the implementations of ComponentCallbacks
// /////////////////////////////////////////////////////////////////////////////
@Override
public void onConfigurationChanged(Configuration newConfiguration) {
super.onConfigurationChanged(newConfiguration);
// This won't happen unless we declare changes we handle in the manifest
if (L)
Log.i(CLASSNAME, "onConfigurationChanged");
}
@Override
public void onLowMemory() {
// No guarantee this is called before or after other callbacks
if (L)
Log.i(CLASSNAME, "onLowMemory");
}
// /////////////////////////////////////////////////////////////////////////////
// ListFragment click handling
// /////////////////////////////////////////////////////////////////////////////
public void onListItemClick(ListView l, View v, int position, long id) {
Cursor c = ((CursorAdapter) getListView().getAdapter()).getCursor();
String item = buildItemInfo(c, position);
String tableInfo = buildDatabaseInfo(c);
Bundle data = ((MainActivity) getActivity()).buildDataBundle(item,
tableInfo);
((TabbedActivity) getActivity()).loadTabFragments(data);
}
// ////////////////////////////////////////////////////////////////////////////
// Implementation of LoaderCallbacks
// ////////////////////////////////////////////////////////////////////////////
// Create the loader, passing in the query
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(this.getActivity(), uriForTable(tableName),
null, null, null, null);
}
// Get results
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
((SimpleCursorAdapter) getListAdapter()).swapCursor(cursor);
getActivity().setProgressBarIndeterminateVisibility(false);
}
// Reset
public void onLoaderReset(Loader<Cursor> loader) {
((SimpleCursorAdapter) getListAdapter()).swapCursor(null);
}
// ////////////////////////////////////////////////////////////////////////////
// App-specific code
// ////////////////////////////////////////////////////////////////////////////
private final static String NL = System.getProperty("line.separator");
/**
* Called from onCreate. restore state if available
*
* @param savedInstanceState
*/
private void doLoaderCreation(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If no saved state, start fresh with the Data table
if (null == savedInstanceState) {
openNewTableByName("android.provider.ContactsContract$Data");
} else {
// See if we can recreate the query from saved state
tableName = savedInstanceState.getString(STATE_LABEL_NAME);
if (null == tableName) {
doLoaderCreation(null); // Nope
} else {
openNewTableByName(tableName);
}
}
}
/**
* Open a new table, based on the name of the class from the API.
*
* @param tableClassName
*/
private void openNewTableByName(String tableClassName) {
Uri table = uriForTable(tableClassName);
if (null == table) {
return;
} // Fail silently
tableName = tableClassName;
newTableQuery(table, ListColumnMap.get(table));
}
/**
* Get the content uri, given the corresponding class name
*
* @param name
* The name of the class in ContactsContract
* @return The content uri for the corresponding class
*/
private Uri uriForTable(String name) {
Class<?> tableClass;
Uri table;
// Get the table's class
try {
tableClass = Class.forName(name);
} catch (ClassNotFoundException e) {
return null;
}
// Get the content Uri, which should be static, hence no instance for
// get
try {
table = (Uri) (tableClass.getDeclaredField("CONTENT_URI").get(null));
} catch (IllegalArgumentException e) {
return null;
} catch (IllegalAccessException e) {
return null;
} catch (NoSuchFieldException e) {
return null;
}
return table;
}
private void newTableQuery(Uri table, String column) {
Activity activity = getActivity();
if (null == column || column.isEmpty()) {
column = BaseColumns._ID;
}
// Show the spinner for this Activity
activity.setProgressBarIndeterminateVisibility(true);
String[] fromColumns = { column };
int[] toViews = { android.R.id.text1 };
// Create an adapter without a cursor
SimpleCursorAdapter adapter = new SimpleCursorAdapter(activity,
android.R.layout.simple_list_item_1, null, fromColumns,
toViews, 0);
setListAdapter(adapter);
// Make a new loader
LoaderManager m = getLoaderManager();
if (null != m.getLoader(LOADER_ID)) {
m.destroyLoader(LOADER_ID);
}
m.initLoader(LOADER_ID, null, this);
}
/**
* Extracts, labels, and formats all the information in all the columns in a
* row.
*
* @param c
* The cursor
* @param position
* The position in the cursor
* @return The formatted data from the row
*/
private String buildItemInfo(Cursor c, int position) {
int i;
int columns = c.getColumnCount();
String info = "";
c.moveToPosition(position);
String names[] = c.getColumnNames();
for (i = 0; i < columns; i++) {
info += names[i] + ": ";
try {
info += c.getString(i);
} catch (Exception e) {
// Fail silently
}
info += NL;
}
return info;
}
private String buildDatabaseInfo(Cursor c) {
String info = "";
info += getString(R.string.column_count_label) + c.getColumnCount()
+ NL;
info += getString(R.string.row_count_label) + c.getCount() + NL;
return info;
}
// /////////////////////////////////////////////////////////////////////////////
// Menu handling code, including implementation of onMenuItemClickListener
// /////////////////////////////////////////////////////////////////////////////
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search_menu, menu);
buildTableMenu(menu);
}
@Override
public boolean onMenuItemClick(MenuItem item) {
openNewTableByName((String) item.getTitle());
return true;
}
// ////////////////////////////////////////////////////////////////////////////
// App-specific code to create a menu of tables
// ////////////////////////////////////////////////////////////////////////////
/**
* Add a MenuItem to the specified menu for each table in the
* ContactsContract class
*
* @param menu
*/
private void buildTableMenu(Menu menu) {
Class<?>[] tablesArray = ContactsContract.class.getClasses();
ArrayList<Class<?>> tablesList = new ArrayList<Class<?>>(
Arrays.asList(tablesArray));
deleteNonTables(tablesList);
for (Class<?> c : tablesList) {
menu.add(c.getName()).setOnMenuItemClickListener(this);
}
}
/**
* Delete the embedded classes of ContactsContract that are not tables i.e.
* not the interfaces, and not the classes that do not implement
* BaseColumns.
*
* This might miss tables that do not, in fact, implement BaseColumns but
* are still tables (but not ones that follow ContactsContract API
* conventions)
*
* @param tablesList
* The raw list of embedded classes
*/
private void deleteNonTables(ArrayList<Class<?>> tablesList) {
ListIterator<Class<?>> l = tablesList.listIterator();
while (l.hasNext()) {
Class<?> c = l.next();
// Might be belt-and-suspenders
if (true == c.isInterface()) {
l.remove();
} else if (false == implementer(c, BaseColumns.class)) {
l.remove();
}
}
}
/**
* Does the specified class implement the specified interface?
*
* @param c
* The class
* @param interf
* The interface
* @return True if the interface is implemented by the class
*/
private boolean implementer(Class<?> c, Class<?> interf) {
for (Class<?> ci : c.getInterfaces()) {
if (ci.equals(interf)) {
return true;
} else {
// Recurse, getInterfaces only gets one level (?!)
if (true == implementer(ci, interf)) {
return true;
}
}
}
return false;
}
}