PageRenderTime 31ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/development/samples/training/device-management-policy/src/com/example/training/deviceadmin/PolicySetupActivity.java

https://gitlab.com/brian0218/rk3288_r-box_android4.4.2_sdk
Java | 275 lines | 200 code | 28 blank | 47 comment | 27 complexity | 115bbf445b8db11b37f9876aa7c94517 MD5 | raw file
  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. package com.example.training.deviceadmin;
  17. import android.app.Activity;
  18. import android.app.admin.DevicePolicyManager;
  19. import android.content.Intent;
  20. import android.content.SharedPreferences;
  21. import android.os.Bundle;
  22. import android.view.View;
  23. import android.widget.AdapterView;
  24. import android.widget.ArrayAdapter;
  25. import android.widget.Button;
  26. import android.widget.EditText;
  27. import android.widget.LinearLayout;
  28. import android.widget.Spinner;
  29. import android.widget.TextView;
  30. /**
  31. * Main Activity for the sample application.
  32. *
  33. * The Activity maintains and presents 2 different
  34. * screens to the user -- a screen for configuring device administration policy and another screen
  35. * for viewing configured policy. When it is detected that the screen-lock password satisfies
  36. * the password strength required by the policy, the user is sent to an Activity containing
  37. * protected content.
  38. */
  39. public class PolicySetupActivity extends Activity {
  40. private static final int REQ_ACTIVATE_DEVICE_ADMIN = 10;
  41. private static final String SCREEN_ID_KEY = "LAYOUT_ID";
  42. private static final String APP_PREF = "APP_PREF";
  43. private static final int UNKNOWN_SCREEN_ID = -1;
  44. // UI controls in policy setup screen.
  45. private Spinner mPasswordQualityInputField;
  46. private EditText mPasswordLengthInputField;
  47. private EditText mPasswordMinUppercaseInputField;
  48. private Policy mPolicy;
  49. private int mCurrentScreenId;
  50. @Override
  51. public void onCreate(Bundle savedInstanceState) {
  52. super.onCreate(savedInstanceState);
  53. mPolicy = new Policy(this);
  54. }
  55. @Override
  56. protected void onResume() {
  57. super.onResume();
  58. SharedPreferences prefs = getSharedPreferences(APP_PREF, MODE_PRIVATE);
  59. final int savedScreenId = prefs.getInt(SCREEN_ID_KEY, UNKNOWN_SCREEN_ID);
  60. if (savedScreenId == UNKNOWN_SCREEN_ID || !mPolicy.isAdminActive()) {
  61. setScreenContent(R.layout.activity_policy_setup);
  62. } else {
  63. setScreenContent(savedScreenId);
  64. }
  65. }
  66. private void setScreenContent(final int screenId) {
  67. mCurrentScreenId = screenId;
  68. setContentView(mCurrentScreenId);
  69. getSharedPreferences(APP_PREF, MODE_PRIVATE).edit().putInt(
  70. SCREEN_ID_KEY, mCurrentScreenId).commit();
  71. switch (mCurrentScreenId) {
  72. case R.layout.activity_policy_setup:
  73. initPolicySetupScreen();
  74. initNavigation();
  75. break;
  76. case R.layout.activity_view_policy:
  77. initViewPolicyScreen();
  78. initNavigation();
  79. break;
  80. }
  81. }
  82. @Override
  83. protected void onPause() {
  84. super.onPause();
  85. if (mCurrentScreenId == R.layout.activity_policy_setup) writePolicy();
  86. }
  87. @Override
  88. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  89. if (requestCode == REQ_ACTIVATE_DEVICE_ADMIN && resultCode == RESULT_OK) {
  90. // User just activated the application as a device administrator.
  91. setScreenContent(mCurrentScreenId);
  92. } else {
  93. super.onActivityResult(requestCode, resultCode, data);
  94. }
  95. }
  96. @Override
  97. public void onBackPressed() {
  98. if (mCurrentScreenId == R.layout.activity_view_policy) {
  99. setScreenContent(R.layout.activity_policy_setup);
  100. return;
  101. }
  102. super.onBackPressed();
  103. }
  104. // Initialize policy set up screen.
  105. private void initPolicySetupScreen() {
  106. mPasswordQualityInputField = (Spinner) findViewById(R.id.policy_password_quality);
  107. mPasswordLengthInputField = (EditText) findViewById(R.id.policy_password_length);
  108. mPasswordMinUppercaseInputField = (EditText) findViewById(R.id.policy_password_uppercase);
  109. ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
  110. R.array.password_types, android.R.layout.simple_spinner_item);
  111. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  112. mPasswordQualityInputField.setAdapter(adapter);
  113. mPasswordQualityInputField.setOnItemSelectedListener(
  114. new AdapterView.OnItemSelectedListener() {
  115. public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
  116. LinearLayout passwordMinUppercaseView =
  117. (LinearLayout) findViewById(R.id.password_uppercase_view);
  118. // The minimum number of upper case field is only applicable for password
  119. // qualities: alpha, alphanumeric, or complex.
  120. if (pos > 2)
  121. passwordMinUppercaseView.setVisibility(View.VISIBLE);
  122. else
  123. passwordMinUppercaseView.setVisibility(View.GONE);
  124. }
  125. public void onNothingSelected(AdapterView<?> parent) {}
  126. });
  127. // Read previously saved policy and populate on the UI.
  128. mPolicy.readFromLocal();
  129. mPasswordQualityInputField.setSelection(mPolicy.getPasswordQuality());
  130. if (mPolicy.getPasswordLength() > 0) {
  131. mPasswordLengthInputField.setText(String.valueOf(mPolicy.getPasswordLength()));
  132. } else {
  133. mPasswordLengthInputField.setText("");
  134. }
  135. if (mPolicy.getPasswordMinUpperCase() > 0) {
  136. mPasswordMinUppercaseInputField.setText(
  137. String.valueOf(mPolicy.getPasswordMinUpperCase()));
  138. } else {
  139. mPasswordMinUppercaseInputField.setText("");
  140. }
  141. }
  142. // Initialize policy viewing screen.
  143. private void initViewPolicyScreen() {
  144. TextView passwordQualityView = (TextView) findViewById(R.id.policy_password_quality);
  145. TextView passwordLengthView = (TextView) findViewById(R.id.policy_password_length);
  146. // Read previously saved policy and populate on the UI.
  147. mPolicy.readFromLocal();
  148. int passwordQualitySelection = mPolicy.getPasswordQuality();
  149. passwordQualityView.setText(
  150. getResources().getStringArray(R.array.password_types)[passwordQualitySelection]);
  151. passwordLengthView.setText(String.valueOf(mPolicy.getPasswordLength()));
  152. if (passwordQualitySelection > 2) {
  153. LinearLayout passwordMinUppercaseView =
  154. (LinearLayout) findViewById(R.id.password_uppercase_view);
  155. passwordMinUppercaseView.setVisibility(View.VISIBLE);
  156. ((TextView) findViewById(R.id.policy_password_uppercase)).setText(
  157. String.valueOf(mPolicy.getPasswordMinUpperCase()));
  158. }
  159. }
  160. // Set up navigation message and action button.
  161. private void initNavigation() {
  162. if (!mPolicy.isAdminActive()) {
  163. // Activates device administrator.
  164. setupNavigation(R.string.setup_message_activate,
  165. R.string.setup_action_activate,
  166. mActivateButtonListener);
  167. } else if (mCurrentScreenId == R.layout.activity_policy_setup) {
  168. setupNavigation(R.string.setup_message_set_policy,
  169. R.string.setup_action_set_policy,
  170. new View.OnClickListener() {
  171. public void onClick(View view) {
  172. writePolicy();
  173. mPolicy.configurePolicy();
  174. setScreenContent(R.layout.activity_view_policy);
  175. }
  176. });
  177. }
  178. else if (!mPolicy.isActivePasswordSufficient()) {
  179. // Launches password set-up screen in Settings.
  180. setupNavigation(R.string.setup_message_enforce_policy,
  181. R.string.setup_action_enforce_policy,
  182. mEnforcePolicyListener);
  183. } else {
  184. // Grants access to secure content.
  185. setupNavigation(R.string.setup_message_go_secured,
  186. R.string.setup_action_go_secured,
  187. new View.OnClickListener() {
  188. public void onClick(View view) {
  189. startActivity(new Intent(view.getContext(), SecureActivity.class));
  190. }
  191. });
  192. }
  193. }
  194. private View.OnClickListener mActivateButtonListener = new View.OnClickListener() {
  195. @Override
  196. public void onClick(View v) {
  197. // First, persist the policy. Then, launch intent to trigger the system screen
  198. // requesting user to confirm the activation of the device administrator.
  199. writePolicy();
  200. Intent activateDeviceAdminIntent =
  201. new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
  202. activateDeviceAdminIntent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
  203. mPolicy.getPolicyAdmin());
  204. // It is good practice to include the optional explanation text to explain to
  205. // user why the application is requesting to be a device administrator. The system
  206. // will display this message on the activation screen.
  207. activateDeviceAdminIntent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
  208. getResources().getString(R.string.device_admin_activation_message));
  209. startActivityForResult(activateDeviceAdminIntent, REQ_ACTIVATE_DEVICE_ADMIN);
  210. }
  211. };
  212. private View.OnClickListener mEnforcePolicyListener = new View.OnClickListener() {
  213. @Override
  214. public void onClick(View v) {
  215. writePolicy();
  216. // The device administration API does not "fix" the password if it is
  217. // determined that the current password does not conform to what is requested
  218. // by the policy. The caller is responsible for triggering the password set up
  219. // screen via the below intent.
  220. Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
  221. startActivity(intent);
  222. }
  223. };
  224. // Setup action button text and listener.
  225. private void setupNavigation(int messageResId, int buttonTextResId,
  226. View.OnClickListener listener) {
  227. TextView setupMessage = (TextView) findViewById(R.id.setup_message);
  228. setupMessage.setText(messageResId);
  229. Button actionBtn = (Button) findViewById(R.id.setup_action_btn);
  230. actionBtn.setText(buttonTextResId);
  231. actionBtn.setOnClickListener(listener);
  232. }
  233. // Save policy to shared preferences.
  234. private void writePolicy() {
  235. int passwordQuality = (int) mPasswordQualityInputField.getSelectedItemId();
  236. int passwordLength = 0;
  237. try {
  238. passwordLength = Integer.valueOf(mPasswordLengthInputField.getText().toString());
  239. } catch (NumberFormatException nfe) {} // Defaults to 0.
  240. int passwordMinUppercase = 0;
  241. try {
  242. passwordMinUppercase =
  243. Integer.valueOf(mPasswordMinUppercaseInputField.getText().toString());
  244. } catch (NumberFormatException nfe) {} // Defaults to 0.
  245. mPolicy.saveToLocal(passwordQuality, passwordLength, passwordMinUppercase);
  246. }
  247. }