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
17package com.android.omadm.service;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Dialog;
22import android.content.DialogInterface;
23import android.content.Intent;
24import android.os.Bundle;
25import android.os.Handler;
26import android.util.Log;
27
28public class DMDialog extends Activity {
29
30    private static final String TAG = "DMDialog";
31
32    private static final int PKG0_ALERT_DLG = 1;
33
34    private static final int PKG0_INFO_DLG = 2;
35
36    private static final int UPDATE_CANCEL_DLG = 3;
37
38    private static final int USER_PRESS_OK = 1;
39
40    private static final int USER_PRESS_CANCEL = 2;
41
42    private String mTitle;
43
44    private String mMessage;
45
46    private int mResult;
47
48    private String mDlgType;
49
50    private AlertDialog mDialog;
51
52    @Override
53    public void onCreate(Bundle savedInstanceState) {
54        super.onCreate(savedInstanceState);
55        if (DMClientService.DBG) {
56            logd("onCreate");
57        }
58        final Intent intent = getIntent();
59        mDlgType = intent.getAction();
60
61        if (mDlgType.equals(DMIntent.SHOW_PKG0_ALERT_DLG)) {
62            mTitle = intent.getStringExtra("Title");
63            mMessage = intent.getStringExtra("Message");
64
65            showDialog(PKG0_ALERT_DLG);
66        }
67
68        if (mDlgType.equals(DMIntent.SHOW_PKG0_INFO_DLG)) {
69            mTitle = intent.getStringExtra("Title");
70            mMessage = intent.getStringExtra("Message");
71            showDialog(PKG0_INFO_DLG);
72
73            Handler handler = new Handler();
74            Runnable closeDialogTask = new Runnable() {
75                @Override
76                public void run() {
77                    mDialog.dismiss();
78                    finish();
79                }
80            };
81
82            handler.postDelayed(closeDialogTask, 40 * 1000);
83        }
84
85        if (mDlgType.equals(DMIntent.SHOW_UPDATE_CANCEL_DLG)) {
86
87            mTitle = intent.getStringExtra("Title");
88            mMessage = intent.getStringExtra("Message");
89            showDialog(UPDATE_CANCEL_DLG);
90        }
91    }
92
93    @Override
94    protected Dialog onCreateDialog(int id) {
95        switch (id) {
96            case PKG0_ALERT_DLG:
97                mDialog = new AlertDialog.Builder(this)
98                        .setTitle(mTitle)
99                        .setIcon(R.drawable.alert_dialog_icon)
100                        .setMessage(mMessage)
101                        .setNegativeButton(android.R.string.cancel, mCancelClickListener)
102                        .setPositiveButton(android.R.string.ok, mOKClickListener)
103                        .setCancelable(false)
104                        .create();
105                return mDialog;
106            case PKG0_INFO_DLG:
107                mDialog = new AlertDialog.Builder(this)
108                        .setTitle(mTitle)
109                        .setIcon(R.drawable.alert_dialog_icon)
110                        .setMessage(mMessage)
111                        .setCancelable(false)
112                        .create();
113                return mDialog;
114            case UPDATE_CANCEL_DLG:
115                mDialog = new AlertDialog.Builder(this)
116                        .setTitle(mTitle)
117                        .setIcon(R.drawable.alert_dialog_icon)
118                        .setMessage(mMessage)
119                        .setPositiveButton(android.R.string.ok, mOKClickListener)
120                        .setCancelable(false)
121                        .create();
122                return mDialog;
123        }
124        return null;
125    }
126
127    private final DialogInterface.OnClickListener mOKClickListener = new DialogInterface.OnClickListener() {
128        @Override
129        public void onClick(DialogInterface dialog, int which) {
130            mResult = USER_PRESS_OK;
131            if (DMClientService.DBG) {
132                logd("Press:OK");
133            }
134            mDialog.dismiss();
135            finish();
136        }
137    };
138
139    private final DialogInterface.OnClickListener mCancelClickListener = new DialogInterface.OnClickListener() {
140        @Override
141        public void onClick(DialogInterface dialog, int which) {
142            mResult = USER_PRESS_CANCEL;
143            if (DMClientService.DBG) {
144                logd("Press:Cancel");
145            }
146            mDialog.dismiss();
147            finish();
148        }
149    };
150
151    @Override
152    public void onDestroy() {
153        Log.i(TAG, "onDestroy");
154        if (mDlgType.equals(DMIntent.SHOW_PKG0_ALERT_DLG)) {
155            Intent i = new Intent(DMIntent.SHOW_PKG0_ALERT_DLG_CLOSE);
156            i.putExtra("Result", mResult);
157            sendBroadcast(i);
158        }
159        super.onDestroy();
160    }
161
162    private static void logd(String msg) {
163        Log.d(TAG, msg);
164    }
165}
166