1 /* 2 * Copyright (C) 2014 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 17 package com.android.omadm.service; 18 19 import android.app.Activity; 20 import android.app.AlertDialog; 21 import android.app.Dialog; 22 import android.app.DialogFragment; 23 import android.content.DialogInterface; 24 import android.content.Intent; 25 import android.os.Bundle; 26 import android.util.Log; 27 28 public class DMSessionConfirmAlertActivity extends Activity { 29 private static final String TAG = "DMSessionConfirmAlertActivity"; 30 private static final boolean DBG = DMClientService.DBG; 31 32 private AlertDialog mDialog; 33 34 @Override 35 public void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 DMHelper.cancelNotification(this, DMHelper.NOTIFICATION_CONFIRMATION_ID); 38 } 39 40 @Override 41 protected void onPause() { 42 super.onPause(); 43 } 44 45 @Override 46 protected void onResume() { 47 logd("Activity DM session confirmation has been started."); 48 super.onResume(); 49 showDialog(); 50 } 51 52 public static class MyAlertDialogFragment extends DialogFragment { 53 54 public static MyAlertDialogFragment newInstance() { 55 MyAlertDialogFragment frag = new MyAlertDialogFragment(); 56 Bundle args = new Bundle(); 57 frag.setArguments(args); 58 return frag; 59 } 60 61 @Override 62 public Dialog onCreateDialog(Bundle savedInstanceState) { 63 64 return new AlertDialog.Builder(getActivity()) 65 .setIcon(R.drawable.alert_dialog_icon) 66 .setTitle(R.string.dm_session_confirmation_title) 67 .setMessage(R.string.dm_session_confirmation_message) 68 .setCancelable(false) 69 .setPositiveButton(getResources().getString(R.string.dm_session_button_confirm), 70 new DialogInterface.OnClickListener() { 71 @Override 72 public void onClick(DialogInterface dialog, int whichButton) { 73 ((DMSessionConfirmAlertActivity) getActivity()) 74 .doPositiveClick(); 75 } 76 } 77 ) 78 .setNegativeButton(getResources().getString(R.string.dm_session_button_reject), 79 new DialogInterface.OnClickListener() { 80 @Override 81 public void onClick(DialogInterface dialog, int whichButton) { 82 ((DMSessionConfirmAlertActivity) getActivity()) 83 .doNegativeClick(); 84 } 85 } 86 ) 87 .create(); 88 } 89 } 90 91 void doNegativeClick() { 92 userDeniedSession(); 93 } 94 95 void doPositiveClick() { 96 userConfirmedSession(); 97 } 98 99 private void userConfirmedSession() { 100 logd("userConfirmedSession"); 101 Intent intent = new Intent(DMIntent.ACTION_USER_CONFIRMED_DM_SESSION); 102 sendBroadcast(intent); 103 104 finishMe(); 105 106 } 107 108 private void userDeniedSession() { 109 logd("userDeniedSession"); 110 DMHelper.cleanAllResources(this); 111 112 finishMe(); 113 114 } 115 116 private void showDialog() { 117 DialogFragment newFragment = MyAlertDialogFragment.newInstance(); 118 newFragment.show(getFragmentManager(), "dialog"); 119 } 120 121 private void finishMe() { 122 123 if (null != mDialog) { 124 mDialog.dismiss(); 125 } 126 127 mDialog = null; 128 finish(); 129 } 130 131 private static void logd(String msg) { 132 Log.d(TAG, msg); 133 } 134 } 135