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
|
/*
* Copyright (C) 2013 SlimRoms Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.privacyguard;
import android.app.FragmentTransaction;
import android.view.animation.AnimationUtils;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AppOpsManager;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.LoaderManager;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Loader;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;
import com.android.settings.R;
import com.android.settings.Settings.AppOpsSummaryActivity;
import com.android.settings.SubSettings;
import com.android.settings.applications.AppOpsDetails;
import com.android.settings.applications.AppOpsState;
import com.android.settings.applications.AppOpsState.OpsTemplate;
import com.android.settings.privacyguard.AppInfoLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PrivacyGuardManager extends Fragment
implements OnItemClickListener, OnItemLongClickListener,
LoaderManager.LoaderCallbacks<List<PrivacyGuardManager.AppInfo>> {
private static final String TAG = "PrivacyGuardManager";
private TextView mNoUserAppsInstalled;
private ListView mAppsList;
private View mLoadingContainer;
private PrivacyGuardAppListAdapter mAdapter;
private List<AppInfo> mApps;
private Activity mActivity;
private SharedPreferences mPreferences;
private AppOpsManager mAppOps;
private int mSavedFirstVisiblePosition = AdapterView.INVALID_POSITION;
private int mSavedFirstItemOffset;
// keys for extras and icicles
private final static String LAST_LIST_POS = "last_list_pos";
private final static String LAST_LIST_OFFSET = "last_list_offset";
// Privacy Guard Fragment
private final static String PRIVACY_GUARD_FRAGMENT_TAG = "privacy_guard_fragment";
// holder for package data passed into the adapter
public static final class AppInfo {
String title;
String packageName;
boolean enabled;
boolean privacyGuardEnabled;
int uid;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mActivity = getActivity();
mAppOps = (AppOpsManager)getActivity().getSystemService(Context.APP_OPS_SERVICE);
View hostView = inflater.inflate(R.layout.privacy_guard_manager, container, false);
Fragment privacyGuardPrefs = PrivacyGuardPrefs.newInstance();
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.privacy_guard_prefs, privacyGuardPrefs,
PRIVACY_GUARD_FRAGMENT_TAG);
fragmentTransaction.commit();
return hostView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mNoUserAppsInstalled = (TextView) mActivity.findViewById(R.id.error);
mAppsList = (ListView) mActivity.findViewById(R.id.apps_list);
mAppsList.setOnItemClickListener(this);
mAppsList.setOnItemLongClickListener(this);
mLoadingContainer = mActivity.findViewById(R.id.loading_container);
// get shared preference
mPreferences = mActivity.getSharedPreferences("privacy_guard_manager", Activity.MODE_PRIVATE);
if (!mPreferences.getBoolean("first_help_shown", false)) {
showHelp();
}
if (savedInstanceState != null) {
mSavedFirstVisiblePosition = savedInstanceState.getInt(LAST_LIST_POS,
AdapterView.INVALID_POSITION);
mSavedFirstItemOffset = savedInstanceState.getInt(LAST_LIST_OFFSET, 0);
} else {
mSavedFirstVisiblePosition = AdapterView.INVALID_POSITION;
mSavedFirstItemOffset = 0;
}
// load apps and construct the list
scheduleAppsLoad();
setHasOptionsMenu(true);
}
@Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(LAST_LIST_POS, mSavedFirstVisiblePosition);
outState.putInt(LAST_LIST_OFFSET, mSavedFirstItemOffset);
}
@Override
public void onPause() {
super.onPause();
// Remember where the list is scrolled to so we can restore the scroll position
// when we come back to this activity and *after* we complete querying for the
// conversations.
mSavedFirstVisiblePosition = mAppsList.getFirstVisiblePosition();
View firstChild = mAppsList.getChildAt(0);
mSavedFirstItemOffset = (firstChild == null) ? 0 : firstChild.getTop();
}
@Override
public void onResume() {
super.onResume();
// rebuild the list; the user might have changed settings inbetween
scheduleAppsLoad();
if (mSavedFirstVisiblePosition != AdapterView.INVALID_POSITION) {
mAppsList.setSelectionFromTop(mSavedFirstVisiblePosition, mSavedFirstItemOffset);
mSavedFirstVisiblePosition = AdapterView.INVALID_POSITION;
}
}
@Override
public Loader<List<AppInfo>> onCreateLoader(int id, Bundle args) {
mLoadingContainer.startAnimation(AnimationUtils.loadAnimation(
mActivity, android.R.anim.fade_in));
mAppsList.startAnimation(AnimationUtils.loadAnimation(
mActivity, android.R.anim.fade_out));
mAppsList.setVisibility(View.INVISIBLE);
mLoadingContainer.setVisibility(View.VISIBLE);
return new AppInfoLoader(mActivity, shouldShowSystemApps());
}
@Override
public void onLoadFinished(Loader<List<AppInfo>> loader, List<AppInfo> apps) {
mApps = apps;
prepareAppAdapter();
mLoadingContainer.startAnimation(AnimationUtils.loadAnimation(
mActivity, android.R.anim.fade_out));
mAppsList.startAnimation(AnimationUtils.loadAnimation(
mActivity, android.R.anim.fade_in));
mLoadingContainer.setVisibility(View.INVISIBLE);
mAppsList.setVisibility(View.VISIBLE);
}
@Override
public void onLoaderReset(Loader<List<AppInfo>> loader) {
}
private void scheduleAppsLoad() {
getLoaderManager().restartLoader(0, null, this);
}
private void prepareAppAdapter() {
// if app list is empty inform the user
// else go ahead and construct the list
if (mApps == null || mApps.isEmpty()) {
mNoUserAppsInstalled.setText(R.string.privacy_guard_no_user_apps);
mNoUserAppsInstalled.setVisibility(View.VISIBLE);
mAppsList.setVisibility(View.GONE);
mAppsList.setAdapter(null);
} else {
mNoUserAppsInstalled.setVisibility(View.GONE);
mAppsList.setVisibility(View.VISIBLE);
mAdapter = createAdapter();
mAppsList.setAdapter(mAdapter);
mAppsList.setFastScrollEnabled(true);
}
}
private PrivacyGuardAppListAdapter createAdapter() {
String lastSectionIndex = null;
ArrayList<String> sections = new ArrayList<String>();
ArrayList<Integer> positions = new ArrayList<Integer>();
int count = mApps.size(), offset = 0;
for (int i = 0; i < count; i++) {
AppInfo app = mApps.get(i);
String sectionIndex;
if (!app.enabled) {
sectionIndex = "--"; //XXX
} else if (app.title.isEmpty()) {
sectionIndex = "";
} else {
sectionIndex = app.title.substring(0, 1).toUpperCase();
}
if (lastSectionIndex == null ||
!TextUtils.equals(sectionIndex, lastSectionIndex)) {
sections.add(sectionIndex);
positions.add(offset);
lastSectionIndex = sectionIndex;
}
offset++;
}
return new PrivacyGuardAppListAdapter(mActivity, mApps, sections, positions);
}
private void resetPrivacyGuard() {
if (mApps == null || mApps.isEmpty()) {
return;
}
showResetDialog();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// on click change the privacy guard status for this item
final AppInfo app = (AppInfo) parent.getItemAtPosition(position);
app.privacyGuardEnabled = !app.privacyGuardEnabled;
mAppOps.setPrivacyGuardSettingForPackage(app.uid, app.packageName, app.privacyGuardEnabled);
mAdapter.notifyDataSetChanged();
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// on long click open app details window
final AppInfo app = (AppInfo) parent.getItemAtPosition(position);
Bundle args = new Bundle();
args.putString(AppOpsDetails.ARG_PACKAGE_NAME, app.packageName);
SubSettings ssa = (SubSettings) getActivity();
ssa.startPreferencePanel(AppOpsDetails.class.getName(), args,
R.string.privacy_guard_manager_title, null, this, 2);
return true;
}
private boolean shouldShowSystemApps() {
return mPreferences.getBoolean("show_system_apps", false);
}
private class HelpDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle(R.string.privacy_guard_help_title)
.setMessage(R.string.privacy_guard_help_text)
.setNegativeButton(R.string.dlg_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.create();
}
@Override
public void onCancel(DialogInterface dialog) {
mPreferences.edit().putBoolean("first_help_shown", true).commit();
}
}
private void showHelp() {
HelpDialogFragment fragment = new HelpDialogFragment();
fragment.show(getFragmentManager(), "help_dialog");
}
private class ResetDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle(R.string.privacy_guard_reset_title)
.setMessage(R.string.privacy_guard_reset_text)
.setPositiveButton(R.string.dlg_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// turn off privacy guard for all apps shown in the current list
for (AppInfo app : mApps) {
app.privacyGuardEnabled = false;
}
mAppOps.resetAllModes();
mAdapter.notifyDataSetChanged();
}
})
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
})
.create();
}
}
private void showResetDialog() {
ResetDialogFragment dialog = new ResetDialogFragment();
dialog.show(getFragmentManager(), "reset_dialog");
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.privacy_guard_manager, menu);
menu.findItem(R.id.show_system_apps).setChecked(shouldShowSystemApps());
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.help:
showHelp();
return true;
case R.id.reset:
resetPrivacyGuard();
return true;
case R.id.show_system_apps:
final String prefName = "show_system_apps";
// set the menu checkbox and save it in
// shared preference and rebuild the list
item.setChecked(!item.isChecked());
mPreferences.edit().putBoolean(prefName, item.isChecked()).commit();
scheduleAppsLoad();
return true;
case R.id.advanced:
Intent i = new Intent(Intent.ACTION_MAIN);
i.setClass(mActivity, AppOpsSummaryActivity.class);
mActivity.startActivity(i);
return true;
default:
return super.onContextItemSelected(item);
}
}
}
|