• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.cellbroadcastreceiver;
18 
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.app.FragmentManager;
22 import android.app.ListFragment;
23 import android.app.LoaderManager;
24 import android.app.NotificationManager;
25 import android.content.Context;
26 import android.content.CursorLoader;
27 import android.content.DialogInterface;
28 import android.content.DialogInterface.OnClickListener;
29 import android.content.Intent;
30 import android.content.Loader;
31 import android.database.Cursor;
32 import android.os.Bundle;
33 import android.os.UserHandle;
34 import android.provider.Telephony;
35 import android.telephony.CellBroadcastMessage;
36 import android.view.ContextMenu;
37 import android.view.ContextMenu.ContextMenuInfo;
38 import android.view.LayoutInflater;
39 import android.view.Menu;
40 import android.view.MenuInflater;
41 import android.view.MenuItem;
42 import android.view.View;
43 import android.view.View.OnCreateContextMenuListener;
44 import android.view.ViewGroup;
45 import android.widget.CursorAdapter;
46 import android.widget.ListView;
47 
48 import java.util.ArrayList;
49 
50 /**
51  * This activity provides a list view of received cell broadcasts. Most of the work is handled
52  * in the inner CursorLoaderListFragment class.
53  */
54 public class CellBroadcastListActivity extends Activity {
55 
56     @Override
onCreate(Bundle savedInstanceState)57     protected void onCreate(Bundle savedInstanceState) {
58         super.onCreate(savedInstanceState);
59 
60         // Dismiss the notification that brought us here (if any).
61         ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
62                 .cancel(CellBroadcastAlertService.NOTIFICATION_ID);
63 
64         FragmentManager fm = getFragmentManager();
65 
66         // Create the list fragment and add it as our sole content.
67         if (fm.findFragmentById(android.R.id.content) == null) {
68             CursorLoaderListFragment listFragment = new CursorLoaderListFragment();
69             fm.beginTransaction().add(android.R.id.content, listFragment).commit();
70         }
71     }
72 
73     /**
74      * List fragment queries SQLite database on worker thread.
75      */
76     public static class CursorLoaderListFragment extends ListFragment
77             implements LoaderManager.LoaderCallbacks<Cursor> {
78 
79         // IDs of the main menu items.
80         private static final int MENU_DELETE_ALL           = 3;
81         private static final int MENU_PREFERENCES          = 4;
82 
83         // IDs of the context menu items (package local, accessed from inner DeleteThreadListener).
84         static final int MENU_DELETE               = 0;
85         static final int MENU_VIEW_DETAILS         = 1;
86 
87         // This is the Adapter being used to display the list's data.
88         CursorAdapter mAdapter;
89 
90         @Override
onCreate(Bundle savedInstanceState)91         public void onCreate(Bundle savedInstanceState) {
92             super.onCreate(savedInstanceState);
93 
94             // We have a menu item to show in action bar.
95             setHasOptionsMenu(true);
96         }
97 
98         @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)99         public View onCreateView(LayoutInflater inflater, ViewGroup container,
100                 Bundle savedInstanceState) {
101             return inflater.inflate(R.layout.cell_broadcast_list_screen, container, false);
102         }
103 
104         @Override
onActivityCreated(Bundle savedInstanceState)105         public void onActivityCreated(Bundle savedInstanceState) {
106             super.onActivityCreated(savedInstanceState);
107 
108             // Set context menu for long-press.
109             ListView listView = getListView();
110             listView.setOnCreateContextMenuListener(mOnCreateContextMenuListener);
111 
112             // Create a cursor adapter to display the loaded data.
113             mAdapter = new CellBroadcastCursorAdapter(getActivity(), null);
114             setListAdapter(mAdapter);
115 
116             // Prepare the loader.  Either re-connect with an existing one,
117             // or start a new one.
118             getLoaderManager().initLoader(0, null, this);
119         }
120 
121         @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)122         public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
123             menu.add(0, MENU_DELETE_ALL, 0, R.string.menu_delete_all).setIcon(
124                     android.R.drawable.ic_menu_delete);
125             if (UserHandle.myUserId() == UserHandle.USER_OWNER) {
126                 menu.add(0, MENU_PREFERENCES, 0, R.string.menu_preferences).setIcon(
127                         android.R.drawable.ic_menu_preferences);
128             }
129         }
130 
131         @Override
onPrepareOptionsMenu(Menu menu)132         public void onPrepareOptionsMenu(Menu menu) {
133             menu.findItem(MENU_DELETE_ALL).setVisible(!mAdapter.isEmpty());
134         }
135 
136         @Override
onListItemClick(ListView l, View v, int position, long id)137         public void onListItemClick(ListView l, View v, int position, long id) {
138             CellBroadcastListItem cbli = (CellBroadcastListItem) v;
139             showDialogAndMarkRead(cbli.getMessage());
140         }
141 
142         @Override
onCreateLoader(int id, Bundle args)143         public Loader<Cursor> onCreateLoader(int id, Bundle args) {
144             return new CursorLoader(getActivity(), CellBroadcastContentProvider.CONTENT_URI,
145                     Telephony.CellBroadcasts.QUERY_COLUMNS, null, null,
146                     Telephony.CellBroadcasts.DELIVERY_TIME + " DESC");
147         }
148 
149         @Override
onLoadFinished(Loader<Cursor> loader, Cursor data)150         public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
151             // Swap the new cursor in.  (The framework will take care of closing the
152             // old cursor once we return.)
153             mAdapter.swapCursor(data);
154             getActivity().invalidateOptionsMenu();
155         }
156 
157         @Override
onLoaderReset(Loader<Cursor> loader)158         public void onLoaderReset(Loader<Cursor> loader) {
159             // This is called when the last Cursor provided to onLoadFinished()
160             // above is about to be closed.  We need to make sure we are no
161             // longer using it.
162             mAdapter.swapCursor(null);
163         }
164 
showDialogAndMarkRead(CellBroadcastMessage cbm)165         private void showDialogAndMarkRead(CellBroadcastMessage cbm) {
166             // show emergency alerts with the warning icon, but don't play alert tone
167             Intent i = new Intent(getActivity(), CellBroadcastAlertDialog.class);
168             ArrayList<CellBroadcastMessage> messageList = new ArrayList<CellBroadcastMessage>(1);
169             messageList.add(cbm);
170             i.putParcelableArrayListExtra(CellBroadcastMessage.SMS_CB_MESSAGE_EXTRA, messageList);
171             startActivity(i);
172         }
173 
showBroadcastDetails(CellBroadcastMessage cbm)174         private void showBroadcastDetails(CellBroadcastMessage cbm) {
175             // show dialog with delivery date/time and alert details
176             CharSequence details = CellBroadcastResources.getMessageDetails(getActivity(), cbm);
177             new AlertDialog.Builder(getActivity())
178                     .setTitle(R.string.view_details_title)
179                     .setMessage(details)
180                     .setCancelable(true)
181                     .show();
182         }
183 
184         private final OnCreateContextMenuListener mOnCreateContextMenuListener =
185                 new OnCreateContextMenuListener() {
186                     @Override
187                     public void onCreateContextMenu(ContextMenu menu, View v,
188                             ContextMenuInfo menuInfo) {
189                         menu.setHeaderTitle(R.string.message_options);
190                         menu.add(0, MENU_VIEW_DETAILS, 0, R.string.menu_view_details);
191                         menu.add(0, MENU_DELETE, 0, R.string.menu_delete);
192                     }
193                 };
194 
195         @Override
onContextItemSelected(MenuItem item)196         public boolean onContextItemSelected(MenuItem item) {
197             Cursor cursor = mAdapter.getCursor();
198             if (cursor != null && cursor.getPosition() >= 0) {
199                 switch (item.getItemId()) {
200                     case MENU_DELETE:
201                         confirmDeleteThread(cursor.getLong(cursor.getColumnIndexOrThrow(
202                                 Telephony.CellBroadcasts._ID)));
203                         break;
204 
205                     case MENU_VIEW_DETAILS:
206                         showBroadcastDetails(CellBroadcastMessage.createFromCursor(cursor));
207                         break;
208 
209                     default:
210                         break;
211                 }
212             }
213             return super.onContextItemSelected(item);
214         }
215 
216         @Override
onOptionsItemSelected(MenuItem item)217         public boolean onOptionsItemSelected(MenuItem item) {
218             switch(item.getItemId()) {
219                 case MENU_DELETE_ALL:
220                     confirmDeleteThread(-1);
221                     break;
222 
223                 case MENU_PREFERENCES:
224                     Intent intent = new Intent(getActivity(), CellBroadcastSettings.class);
225                     startActivity(intent);
226                     break;
227 
228                 default:
229                     return true;
230             }
231             return false;
232         }
233 
234         /**
235          * Start the process of putting up a dialog to confirm deleting a broadcast.
236          * @param rowId the row ID of the broadcast to delete, or -1 to delete all broadcasts
237          */
confirmDeleteThread(long rowId)238         public void confirmDeleteThread(long rowId) {
239             DeleteThreadListener listener = new DeleteThreadListener(rowId);
240             confirmDeleteThreadDialog(listener, (rowId == -1), getActivity());
241         }
242 
243         /**
244          * Build and show the proper delete broadcast dialog. The UI is slightly different
245          * depending on whether there are locked messages in the thread(s) and whether we're
246          * deleting a single broadcast or all broadcasts.
247          * @param listener gets called when the delete button is pressed
248          * @param deleteAll whether to show a single thread or all threads UI
249          * @param context used to load the various UI elements
250          */
confirmDeleteThreadDialog(DeleteThreadListener listener, boolean deleteAll, Context context)251         public static void confirmDeleteThreadDialog(DeleteThreadListener listener,
252                 boolean deleteAll, Context context) {
253             AlertDialog.Builder builder = new AlertDialog.Builder(context);
254             builder.setIconAttribute(android.R.attr.alertDialogIcon)
255                     .setCancelable(true)
256                     .setPositiveButton(R.string.button_delete, listener)
257                     .setNegativeButton(R.string.button_cancel, null)
258                     .setMessage(deleteAll ? R.string.confirm_delete_all_broadcasts
259                             : R.string.confirm_delete_broadcast)
260                     .show();
261         }
262 
263         public class DeleteThreadListener implements OnClickListener {
264             private final long mRowId;
265 
DeleteThreadListener(long rowId)266             public DeleteThreadListener(long rowId) {
267                 mRowId = rowId;
268             }
269 
270             @Override
onClick(DialogInterface dialog, int whichButton)271             public void onClick(DialogInterface dialog, int whichButton) {
272                 // delete from database on a background thread
273                 new CellBroadcastContentProvider.AsyncCellBroadcastTask(
274                         getActivity().getContentResolver()).execute(
275                         new CellBroadcastContentProvider.CellBroadcastOperation() {
276                             @Override
277                             public boolean execute(CellBroadcastContentProvider provider) {
278                                 if (mRowId != -1) {
279                                     return provider.deleteBroadcast(mRowId);
280                                 } else {
281                                     return provider.deleteAllBroadcasts();
282                                 }
283                             }
284                         });
285 
286                 dialog.dismiss();
287             }
288         }
289     }
290 }
291