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
|
/*
* Copyright (C) 2010 The Android Open Source 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;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.app.ActivityManagerNative;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.admin.DeviceAdminInfo;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.RemoteCallback;
import android.os.RemoteException;
import android.text.TextUtils.TruncateAt;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AppSecurityPermissions;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.IOException;
import java.util.ArrayList;
public class DeviceAdminAdd extends Activity {
static final String TAG = "DeviceAdminAdd";
static final int DIALOG_WARNING = 1;
private static final int MAX_ADD_MSG_LINES_PORTRAIT = 5;
private static final int MAX_ADD_MSG_LINES_LANDSCAPE = 2;
private static final int MAX_ADD_MSG_LINES = 15;
Handler mHandler;
DevicePolicyManager mDPM;
DeviceAdminInfo mDeviceAdmin;
CharSequence mAddMsgText;
ImageView mAdminIcon;
TextView mAdminName;
TextView mAdminDescription;
TextView mAddMsg;
ImageView mAddMsgExpander;
boolean mAddMsgEllipsized = true;
TextView mAdminWarning;
ViewGroup mAdminPolicies;
Button mActionButton;
Button mCancelButton;
final ArrayList<View> mAddingPolicies = new ArrayList<View>();
final ArrayList<View> mActivePolicies = new ArrayList<View>();
boolean mAdding;
boolean mRefreshing;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mHandler = new Handler(getMainLooper());
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
if ((getIntent().getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Log.w(TAG, "Cannot start ADD_DEVICE_ADMIN as a new task");
finish();
return;
}
ComponentName cn = (ComponentName)getIntent().getParcelableExtra(
DevicePolicyManager.EXTRA_DEVICE_ADMIN);
if (cn == null) {
Log.w(TAG, "No component specified in " + getIntent().getAction());
finish();
return;
}
ActivityInfo ai;
try {
ai = getPackageManager().getReceiverInfo(cn, PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
Log.w(TAG, "Unable to retrieve device policy " + cn, e);
finish();
return;
}
ResolveInfo ri = new ResolveInfo();
ri.activityInfo = ai;
try {
mDeviceAdmin = new DeviceAdminInfo(this, ri);
} catch (XmlPullParserException e) {
Log.w(TAG, "Unable to retrieve device policy " + cn, e);
finish();
return;
} catch (IOException e) {
Log.w(TAG, "Unable to retrieve device policy " + cn, e);
finish();
return;
}
// This admin already exists, an we have two options at this point. If new policy
// bits are set, show the user the new list. If nothing has changed, simply return
// "OK" immediately.
if (DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN.equals(getIntent().getAction())) {
mRefreshing = false;
if (mDPM.isAdminActive(cn)) {
ArrayList<DeviceAdminInfo.PolicyInfo> newPolicies = mDeviceAdmin.getUsedPolicies();
for (int i = 0; i < newPolicies.size(); i++) {
DeviceAdminInfo.PolicyInfo pi = newPolicies.get(i);
if (!mDPM.hasGrantedPolicy(cn, pi.ident)) {
mRefreshing = true;
break;
}
}
if (!mRefreshing) {
// Nothing changed (or policies were removed) - return immediately
setResult(Activity.RESULT_OK);
finish();
return;
}
}
}
mAddMsgText = getIntent().getCharSequenceExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION);
setContentView(R.layout.device_admin_add);
mAdminIcon = (ImageView)findViewById(R.id.admin_icon);
mAdminName = (TextView)findViewById(R.id.admin_name);
mAdminDescription = (TextView)findViewById(R.id.admin_description);
mAddMsg = (TextView)findViewById(R.id.add_msg);
mAddMsgExpander = (ImageView) findViewById(R.id.add_msg_expander);
mAddMsg.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
toggleMessageEllipsis(v);
}
});
// toggleMessageEllipsis also handles initial layout:
toggleMessageEllipsis(mAddMsg);
mAdminWarning = (TextView) findViewById(R.id.admin_warning);
mAdminPolicies = (ViewGroup) findViewById(R.id.admin_policies);
mCancelButton = (Button) findViewById(R.id.cancel_button);
mCancelButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
mActionButton = (Button) findViewById(R.id.action_button);
mActionButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mAdding) {
try {
mDPM.setActiveAdmin(mDeviceAdmin.getComponent(), mRefreshing);
setResult(Activity.RESULT_OK);
} catch (RuntimeException e) {
// Something bad happened... could be that it was
// already set, though.
Log.w(TAG, "Exception trying to activate admin "
+ mDeviceAdmin.getComponent(), e);
if (mDPM.isAdminActive(mDeviceAdmin.getComponent())) {
setResult(Activity.RESULT_OK);
}
}
finish();
} else {
try {
// Don't allow the admin to put a dialog up in front
// of us while we interact with the user.
ActivityManagerNative.getDefault().stopAppSwitches();
} catch (RemoteException e) {
}
mDPM.getRemoveWarning(mDeviceAdmin.getComponent(),
new RemoteCallback(mHandler) {
@Override
protected void onResult(Bundle bundle) {
CharSequence msg = bundle != null
? bundle.getCharSequence(
DeviceAdminReceiver.EXTRA_DISABLE_WARNING)
: null;
if (msg == null) {
try {
ActivityManagerNative.getDefault().resumeAppSwitches();
} catch (RemoteException e) {
}
mDPM.removeActiveAdmin(mDeviceAdmin.getComponent());
finish();
} else {
Bundle args = new Bundle();
args.putCharSequence(
DeviceAdminReceiver.EXTRA_DISABLE_WARNING, msg);
showDialog(DIALOG_WARNING, args);
}
}
});
}
}
});
}
@Override
protected void onResume() {
super.onResume();
updateInterface();
}
@Override
protected Dialog onCreateDialog(int id, Bundle args) {
switch (id) {
case DIALOG_WARNING: {
CharSequence msg = args.getCharSequence(DeviceAdminReceiver.EXTRA_DISABLE_WARNING);
AlertDialog.Builder builder = new AlertDialog.Builder(
DeviceAdminAdd.this);
builder.setMessage(msg);
builder.setPositiveButton(R.string.dlg_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mDPM.removeActiveAdmin(mDeviceAdmin.getComponent());
finish();
}
});
builder.setNegativeButton(R.string.dlg_cancel, null);
return builder.create();
}
default:
return super.onCreateDialog(id, args);
}
}
static void setViewVisibility(ArrayList<View> views, int visibility) {
final int N = views.size();
for (int i=0; i<N; i++) {
views.get(i).setVisibility(visibility);
}
}
void updateInterface() {
mAdminIcon.setImageDrawable(mDeviceAdmin.loadIcon(getPackageManager()));
mAdminName.setText(mDeviceAdmin.loadLabel(getPackageManager()));
try {
mAdminDescription.setText(
mDeviceAdmin.loadDescription(getPackageManager()));
mAdminDescription.setVisibility(View.VISIBLE);
} catch (Resources.NotFoundException e) {
mAdminDescription.setVisibility(View.GONE);
}
if (mAddMsgText != null) {
mAddMsg.setText(mAddMsgText);
mAddMsg.setVisibility(View.VISIBLE);
} else {
mAddMsg.setVisibility(View.GONE);
mAddMsgExpander.setVisibility(View.GONE);
}
if (!mRefreshing && mDPM.isAdminActive(mDeviceAdmin.getComponent())) {
if (mActivePolicies.size() == 0) {
ArrayList<DeviceAdminInfo.PolicyInfo> policies = mDeviceAdmin.getUsedPolicies();
for (int i=0; i<policies.size(); i++) {
DeviceAdminInfo.PolicyInfo pi = policies.get(i);
View view = AppSecurityPermissions.getPermissionItemView(
this, getText(pi.label), "", true);
mActivePolicies.add(view);
mAdminPolicies.addView(view);
}
}
setViewVisibility(mActivePolicies, View.VISIBLE);
setViewVisibility(mAddingPolicies, View.GONE);
mAdminWarning.setText(getString(R.string.device_admin_status,
mDeviceAdmin.getActivityInfo().applicationInfo.loadLabel(getPackageManager())));
setTitle(getText(R.string.active_device_admin_msg));
mActionButton.setText(getText(R.string.remove_device_admin));
mAdding = false;
} else {
if (mAddingPolicies.size() == 0) {
ArrayList<DeviceAdminInfo.PolicyInfo> policies = mDeviceAdmin.getUsedPolicies();
for (int i=0; i<policies.size(); i++) {
DeviceAdminInfo.PolicyInfo pi = policies.get(i);
View view = AppSecurityPermissions.getPermissionItemView(
this, getText(pi.label), getText(pi.description), true);
mAddingPolicies.add(view);
mAdminPolicies.addView(view);
}
}
setViewVisibility(mAddingPolicies, View.VISIBLE);
setViewVisibility(mActivePolicies, View.GONE);
mAdminWarning.setText(getString(R.string.device_admin_warning,
mDeviceAdmin.getActivityInfo().applicationInfo.loadLabel(getPackageManager())));
setTitle(getText(R.string.add_device_admin_msg));
mActionButton.setText(getText(R.string.add_device_admin));
mAdding = true;
}
}
void toggleMessageEllipsis(View v) {
TextView tv = (TextView) v;
mAddMsgEllipsized = ! mAddMsgEllipsized;
tv.setEllipsize(mAddMsgEllipsized ? TruncateAt.END : null);
tv.setMaxLines(mAddMsgEllipsized ? getEllipsizedLines() : MAX_ADD_MSG_LINES);
mAddMsgExpander.setImageResource(mAddMsgEllipsized ?
com.android.internal.R.drawable.expander_ic_minimized :
com.android.internal.R.drawable.expander_ic_maximized);
}
int getEllipsizedLines() {
Display d = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
return d.getHeight() > d.getWidth() ?
MAX_ADD_MSG_LINES_PORTRAIT : MAX_ADD_MSG_LINES_LANDSCAPE;
}
}
|