development
修订版 | 36a3d06181c35f0f5ff42338b2995bd764deddd1 (tree) |
---|---|
时间 | 2011-01-13 14:19:06 |
作者 | Andy Stadler <stadler@goog...> |
Commiter | Android Git Automerger |
am 3eb48a06: Add encryption status controls
* commit '3eb48a061aa2d6aafe7493bb3d83ea8f8d53a33d':
@@ -272,6 +272,48 @@ | ||
272 | 272 | |
273 | 273 | </LinearLayout> |
274 | 274 | |
275 | + <!-- Encryption Status Controls --> | |
276 | + <LinearLayout | |
277 | + android:orientation="horizontal" | |
278 | + android:gravity="center" | |
279 | + android:layout_width="match_parent" | |
280 | + android:layout_height="wrap_content" > | |
281 | + | |
282 | + <Button android:id="@+id/encryption_enable_button" | |
283 | + android:layout_width="wrap_content" | |
284 | + android:layout_height="wrap_content" | |
285 | + android:text="@string/encryption_enable_label" /> | |
286 | + | |
287 | + <Button android:id="@+id/encryption_disable_button" | |
288 | + android:layout_width="wrap_content" | |
289 | + android:layout_height="wrap_content" | |
290 | + android:text="@string/encryption_disable_label" /> | |
291 | + | |
292 | + <Button android:id="@+id/encryption_activate_button" | |
293 | + android:layout_width="wrap_content" | |
294 | + android:layout_height="wrap_content" | |
295 | + android:text="@string/encryption_activate_label" /> | |
296 | + | |
297 | + </LinearLayout> | |
298 | + | |
299 | + <LinearLayout | |
300 | + android:orientation="horizontal" | |
301 | + android:gravity="center" | |
302 | + android:layout_width="match_parent" | |
303 | + android:layout_height="wrap_content" > | |
304 | + | |
305 | + <TextView android:id="@+id/encryption_status" | |
306 | + android:layout_width="wrap_content" | |
307 | + android:layout_height="wrap_content" /> | |
308 | + | |
309 | + <Button android:id="@+id/encryption_update_status_button" | |
310 | + android:layout_width="wrap_content" | |
311 | + android:layout_height="wrap_content" | |
312 | + android_layout_gravity="east|center_vertical" | |
313 | + android:text="@string/update_encryption_status_label" /> | |
314 | + | |
315 | + </LinearLayout> | |
316 | + | |
275 | 317 | </LinearLayout> |
276 | 318 | |
277 | 319 | </ScrollView> |
@@ -614,6 +614,11 @@ | ||
614 | 614 | <string name="proxylist_hint">No proxy for domain1,domain2</string> |
615 | 615 | <string name="set_proxy_label">Set Global Proxy</string> |
616 | 616 | |
617 | + <string name="encryption_enable_label">Enable Encryption</string> | |
618 | + <string name="encryption_disable_label">Disable Encryption</string> | |
619 | + <string name="encryption_activate_label">Activate Encryption</string> | |
620 | + <string name="update_encryption_status_label">Update Status</string> | |
621 | + | |
617 | 622 | <!-- ============================== --> |
618 | 623 | <!-- app/voice recognition examples strings --> |
619 | 624 | <!-- ============================== --> |
@@ -24,6 +24,7 @@ | ||
24 | 24 | <wipe-data /> |
25 | 25 | <set-global-proxy /> |
26 | 26 | <expire-password /> |
27 | + <encrypted-storage /> | |
27 | 28 | </uses-policies> |
28 | 29 | </device-admin> |
29 | 30 | <!-- END_INCLUDE(meta_data) --> |
@@ -141,7 +141,10 @@ public class DeviceAdminSample extends DeviceAdminReceiver { | ||
141 | 141 | * all together; typically this code would appear in some separate class. |
142 | 142 | */ |
143 | 143 | public static class Controller extends Activity { |
144 | - static final int RESULT_ENABLE = 1; | |
144 | + | |
145 | + static final int REQUEST_CODE_ENABLE_ADMIN = 1; | |
146 | + static final int REQUEST_CODE_START_ENCRYPTION = 2; | |
147 | + | |
145 | 148 | private static final long MS_PER_MINUTE = 60*1000; |
146 | 149 | |
147 | 150 | DevicePolicyManager mDPM; |
@@ -195,6 +198,12 @@ public class DeviceAdminSample extends DeviceAdminReceiver { | ||
195 | 198 | private TextView mPasswordExpirationStatus; |
196 | 199 | private Button mPasswordExpirationStatusButton; |
197 | 200 | |
201 | + private Button mEnableEncryptionButton; | |
202 | + private Button mDisableEncryptionButton; | |
203 | + private Button mActivateEncryptionButton; | |
204 | + private Button mEncryptionStatusButton; | |
205 | + private TextView mEncryptionStatus; | |
206 | + | |
198 | 207 | @Override |
199 | 208 | protected void onCreate(Bundle savedInstanceState) { |
200 | 209 | super.onCreate(savedInstanceState); |
@@ -395,6 +404,16 @@ public class DeviceAdminSample extends DeviceAdminReceiver { | ||
395 | 404 | mProxyList = (EditText) findViewById(R.id.proxylist); |
396 | 405 | mProxyButton = (Button) findViewById(R.id.set_proxy); |
397 | 406 | mProxyButton.setOnClickListener(mSetProxyListener); |
407 | + | |
408 | + mEnableEncryptionButton = (Button) findViewById(R.id.encryption_enable_button); | |
409 | + mEnableEncryptionButton.setOnClickListener(mEncryptionButtonListener); | |
410 | + mDisableEncryptionButton = (Button) findViewById(R.id.encryption_disable_button); | |
411 | + mDisableEncryptionButton.setOnClickListener(mEncryptionButtonListener); | |
412 | + mActivateEncryptionButton = (Button) findViewById(R.id.encryption_activate_button); | |
413 | + mActivateEncryptionButton.setOnClickListener(mEncryptionButtonListener); | |
414 | + mEncryptionStatusButton = (Button) findViewById(R.id.encryption_update_status_button); | |
415 | + mEncryptionStatusButton.setOnClickListener(mEncryptionButtonListener); | |
416 | + mEncryptionStatus = (TextView) findViewById(R.id.encryption_status); | |
398 | 417 | } |
399 | 418 | |
400 | 419 | void updateButtonStates() { |
@@ -417,6 +436,10 @@ public class DeviceAdminSample extends DeviceAdminReceiver { | ||
417 | 436 | mForceLockButton.setEnabled(true); |
418 | 437 | mWipeDataButton.setEnabled(true); |
419 | 438 | mWipeAllDataButton.setEnabled(true); |
439 | + mEnableEncryptionButton.setEnabled(true); | |
440 | + mDisableEncryptionButton.setEnabled(true); | |
441 | + mActivateEncryptionButton.setEnabled(true); | |
442 | + mEncryptionStatusButton.setEnabled(true); | |
420 | 443 | } else { |
421 | 444 | mEnableButton.setEnabled(true); |
422 | 445 | mDisableButton.setEnabled(false); |
@@ -435,6 +458,10 @@ public class DeviceAdminSample extends DeviceAdminReceiver { | ||
435 | 458 | mForceLockButton.setEnabled(false); |
436 | 459 | mWipeDataButton.setEnabled(false); |
437 | 460 | mWipeAllDataButton.setEnabled(false); |
461 | + mEnableEncryptionButton.setEnabled(false); | |
462 | + mDisableEncryptionButton.setEnabled(false); | |
463 | + mActivateEncryptionButton.setEnabled(false); | |
464 | + mEncryptionStatusButton.setEnabled(false); | |
438 | 465 | } |
439 | 466 | } |
440 | 467 |
@@ -622,13 +649,16 @@ public class DeviceAdminSample extends DeviceAdminReceiver { | ||
622 | 649 | @Override |
623 | 650 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
624 | 651 | switch (requestCode) { |
625 | - case RESULT_ENABLE: | |
652 | + case REQUEST_CODE_ENABLE_ADMIN: | |
626 | 653 | if (resultCode == Activity.RESULT_OK) { |
627 | 654 | Log.i(TAG, "Admin enabled!"); |
628 | 655 | } else { |
629 | 656 | Log.i(TAG, "Admin enable FAILED!"); |
630 | 657 | } |
631 | 658 | return; |
659 | + case REQUEST_CODE_START_ENCRYPTION: | |
660 | + updateEncryptionStatus(); | |
661 | + return; | |
632 | 662 | } |
633 | 663 | |
634 | 664 | super.onActivityResult(requestCode, resultCode, data); |
@@ -642,7 +672,7 @@ public class DeviceAdminSample extends DeviceAdminReceiver { | ||
642 | 672 | mDeviceAdminSample); |
643 | 673 | intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, |
644 | 674 | "Additional text explaining why this needs to be added."); |
645 | - startActivityForResult(intent, RESULT_ENABLE); | |
675 | + startActivityForResult(intent, REQUEST_CODE_ENABLE_ADMIN); | |
646 | 676 | } |
647 | 677 | }; |
648 | 678 |
@@ -801,5 +831,62 @@ public class DeviceAdminSample extends DeviceAdminReceiver { | ||
801 | 831 | } |
802 | 832 | }; |
803 | 833 | |
834 | + private OnClickListener mEncryptionButtonListener = new OnClickListener() { | |
835 | + public void onClick(View v) { | |
836 | + int buttonId = v.getId(); | |
837 | + if (buttonId == R.id.encryption_enable_button) { | |
838 | + mDPM.setStorageEncryption(mDeviceAdminSample, true); | |
839 | + } else if (buttonId == R.id.encryption_disable_button) { | |
840 | + mDPM.setStorageEncryption(mDeviceAdminSample, false); | |
841 | + } else if (buttonId == R.id.encryption_activate_button) { | |
842 | + if (ActivityManager.isUserAMonkey()) { | |
843 | + // Don't trust monkeys to do the right thing! | |
844 | + AlertDialog.Builder builder = new AlertDialog.Builder(Controller.this); | |
845 | + builder.setMessage("You can't activate encryption, you're a monkey!"); | |
846 | + builder.setPositiveButton("I admit defeat", null); | |
847 | + builder.show(); | |
848 | + return; | |
849 | + } | |
850 | + if (mDPM.getStorageEncryption(mDeviceAdminSample) == | |
851 | + DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED) { | |
852 | + AlertDialog.Builder builder = new AlertDialog.Builder(Controller.this); | |
853 | + builder.setMessage("Encryption is unsupported on this device."); | |
854 | + builder.setPositiveButton("OK", null); | |
855 | + builder.show(); | |
856 | + return; | |
857 | + } | |
858 | + // Launch the activity to activate encryption. May or may not return! | |
859 | + Intent intent = new Intent(DevicePolicyManager.ACTION_START_ENCRYPTION); | |
860 | + startActivityForResult(intent, REQUEST_CODE_START_ENCRYPTION); | |
861 | + } | |
862 | + | |
863 | + // In all cases, fall through to update status | |
864 | + updateEncryptionStatus(); | |
865 | + } | |
866 | + }; | |
867 | + | |
868 | + private void updateEncryptionStatus() { | |
869 | + String newStatus = "unknown"; | |
870 | + int newStatusCode = mDPM.getStorageEncryption(mDeviceAdminSample); | |
871 | + switch (newStatusCode) { | |
872 | + case DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED: | |
873 | + newStatus = "unsupported"; | |
874 | + break; | |
875 | + case DevicePolicyManager.ENCRYPTION_STATUS_INACTIVE: | |
876 | + newStatus = "inactive"; | |
877 | + break; | |
878 | + case DevicePolicyManager.ENCRYPTION_STATUS_REQUESTED: | |
879 | + newStatus = "requested"; | |
880 | + break; | |
881 | + case DevicePolicyManager.ENCRYPTION_STATUS_ACTIVATING: | |
882 | + newStatus = "activating"; | |
883 | + break; | |
884 | + case DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE: | |
885 | + newStatus = "active"; | |
886 | + break; | |
887 | + } | |
888 | + mEncryptionStatus.setText(newStatus); | |
889 | + } | |
804 | 890 | } |
805 | 891 | } |
892 | + |