1 /*
2  * Copyright (C) 2009 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.phone;
18 
19 import android.app.AlertDialog;
20 import android.content.Context;
21 import android.os.SystemProperties;
22 import android.util.Log;
23 import android.view.WindowManager;
24 
25 /**
26  * Helper class for displaying the a string triggered by a lower level Phone request
27  */
28 public class PhoneDisplayMessage {
29     private static final String LOG_TAG = "PhoneDisplayMessage";
30     private static final boolean DBG = (SystemProperties.getInt("ro.debuggable", 0) == 1);
31 
32     /** Display message dialog */
33     private static AlertDialog sDisplayMessageDialog = null;
34 
35     /**
36      * Display the alert dialog with the network message.
37      *
38      * @param context context to get strings.
39      * @param infoMsg Text message from Network.
40      */
displayNetworkMessage(Context context, String infoMsg)41     public static void displayNetworkMessage(Context context, String infoMsg) {
42         if (DBG) log("displayInfoRecord: infoMsg=" + infoMsg);
43 
44         String title = (String)context.getText(R.string.network_info_message);
45         displayMessage(context, title, infoMsg);
46     }
47 
48     /**
49      * Display the alert dialog with the error message.
50      *
51      * @param context context to get strings.
52      * @param errorMsg Error message describing the network triggered error
53      */
displayErrorMessage(Context context, String errorMsg)54     public static void displayErrorMessage(Context context, String errorMsg) {
55         if (DBG) log("displayErrorMessage: errorMsg=" + errorMsg);
56 
57         String title = (String)context.getText(R.string.network_error_message);
58         displayMessage(context, title, errorMsg);
59     }
60 
displayMessage(Context context, String title, String msg)61     public static void displayMessage(Context context, String title, String msg) {
62         if (DBG) log("displayMessage: msg=" + msg);
63 
64         if (sDisplayMessageDialog != null) {
65             sDisplayMessageDialog.dismiss();
66         }
67 
68         // displaying system alert dialog on the screen instead of
69         // using another activity to display the message.  This
70         // places the message at the forefront of the UI.
71         sDisplayMessageDialog = new AlertDialog.Builder(context)
72                 .setIcon(android.R.drawable.ic_dialog_info)
73                 .setTitle(title)
74                 .setMessage(msg)
75                 .setCancelable(true)
76                 .create();
77 
78         sDisplayMessageDialog.getWindow().setType(
79                 WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
80         sDisplayMessageDialog.getWindow().addFlags(
81                 WindowManager.LayoutParams.FLAG_DIM_BEHIND);
82 
83         sDisplayMessageDialog.show();
84         PhoneGlobals.getInstance().wakeUpScreen();
85     }
86 
87     /**
88      * Dismiss the DisplayInfo record
89      */
dismissMessage()90     public static void dismissMessage() {
91         if (DBG) log("Dissmissing Display Info Record...");
92 
93         if (sDisplayMessageDialog != null) {
94             sDisplayMessageDialog.dismiss();
95             sDisplayMessageDialog = null;
96         }
97     }
98 
log(String msg)99     private static void log(String msg) {
100         Log.d(LOG_TAG, "[PhoneDisplayMessage] " + msg);
101     }
102 }
103