1 /* 2 * Copyright (C) 2017 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.android.datawiper; 17 18 import android.app.Activity; 19 import android.app.AlertDialog; 20 import android.app.admin.DevicePolicyManager; 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.content.DialogInterface; 24 import android.content.Intent; 25 import android.os.Bundle; 26 import android.util.Log; 27 import android.view.View; 28 import android.widget.TextView; 29 30 public class MyMain extends Activity { 31 private static final int REQUEST_CODE_ENABLE_ADMIN = 1; 32 33 @Override onCreate(Bundle savedInstanceState)34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 37 setContentView(R.layout.main); 38 } 39 40 @Override onResume()41 protected void onResume() { 42 super.onResume(); 43 44 refreshUi(); 45 } 46 47 @Override onActivityResult(int requestCode, int resultCode, Intent data)48 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 49 super.onActivityResult(requestCode, resultCode, data); 50 51 refreshUi(); 52 } 53 refreshUi()54 private void refreshUi() { 55 final boolean isEnabled = MyAdmin.isEnabled(this); 56 ((TextView) findViewById(R.id.add_as_admin)).setText( 57 isEnabled ? "Disable Admin" : "Enable Admin"); 58 findViewById(R.id.wipe_data).setEnabled(isEnabled); 59 } 60 onAddAsAdmin(View v)61 public void onAddAsAdmin(View v) { 62 if (MyAdmin.isEnabled(this)) { 63 MyAdmin.disable(this); 64 } else { 65 enableAdmin(); 66 } 67 refreshUi(); 68 } 69 enableAdmin()70 private void enableAdmin() { 71 Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); 72 intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, MyAdmin.getComponent(this)); 73 startActivityForResult(intent, REQUEST_CODE_ENABLE_ADMIN); 74 } 75 restart(Context context)76 public static void restart(Context context) { 77 Intent intent = new Intent().setComponent(new ComponentName(context, MyMain.class)) 78 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 79 | Intent.FLAG_ACTIVITY_CLEAR_TASK); 80 context.startActivity(intent); 81 } 82 onWipeData(View v)83 public void onWipeData(View v) { 84 new AlertDialog.Builder(this) 85 .setIcon(android.R.drawable.ic_dialog_alert) 86 .setTitle("WARNING") 87 .setMessage("This will wipe the device. Are you sure? Do you want to proceed?" 88 + "THIS CANNOT BE UNDONE.") 89 .setCancelable(true) 90 .setPositiveButton("WIPE NOW", new DialogInterface.OnClickListener() { 91 @Override 92 public void onClick(DialogInterface dialog, int which) { 93 Log.w("DataWiper", "Wiping..."); 94 wipe(); 95 } 96 }) 97 .show(); 98 } 99 wipe()100 private void wipe() { 101 ((DevicePolicyManager) (getSystemService(Context.DEVICE_POLICY_SERVICE))) 102 .wipeData(0); 103 } 104 } 105