1 /*
2  * Copyright (C) 2007 Google Inc.
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.internal.app;
18 
19 import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
20 
21 import android.app.AlertDialog;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.location.LocationManagerInternal;
26 import android.os.Bundle;
27 import android.os.Handler;
28 import android.os.Message;
29 import android.util.Log;
30 
31 import com.android.internal.R;
32 import com.android.internal.location.GpsNetInitiatedHandler;
33 import com.android.server.LocalServices;
34 
35 /**
36  * This activity is shown to the user for them to accept or deny network-initiated
37  * requests. It uses the alert dialog style. It will be launched from a notification.
38  */
39 public class NetInitiatedActivity extends AlertActivity implements DialogInterface.OnClickListener {
40 
41     private static final String TAG = "NetInitiatedActivity";
42 
43     private static final boolean DEBUG = true;
44 
45     private static final int POSITIVE_BUTTON = AlertDialog.BUTTON_POSITIVE;
46     private static final int NEGATIVE_BUTTON = AlertDialog.BUTTON_NEGATIVE;
47 
48     private static final int GPS_NO_RESPONSE_TIME_OUT = 1;
49     // Received ID from intent, -1 when no notification is in progress
50     private int notificationId = -1;
51     private int timeout = -1;
52     private int default_response = -1;
53     private int default_response_timeout = 6;
54 
55     private final Handler mHandler = new Handler() {
56         public void handleMessage(Message msg) {
57             switch (msg.what) {
58                 case GPS_NO_RESPONSE_TIME_OUT: {
59                     if (notificationId != -1) {
60                         sendUserResponse(default_response);
61                     }
62                     finish();
63                 }
64                 break;
65                 default:
66             }
67         }
68     };
69 
70     @Override
onCreate(Bundle savedInstanceState)71     protected void onCreate(Bundle savedInstanceState) {
72         super.onCreate(savedInstanceState);
73 
74         getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
75 
76         // Set up the "dialog"
77         final Intent intent = getIntent();
78         final AlertController.AlertParams p = mAlertParams;
79         Context context = getApplicationContext();
80         p.mTitle = intent.getStringExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_TITLE);
81         p.mMessage = intent.getStringExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_MESSAGE);
82         p.mPositiveButtonText = String.format(context.getString(R.string.gpsVerifYes));
83         p.mPositiveButtonListener = this;
84         p.mNegativeButtonText = String.format(context.getString(R.string.gpsVerifNo));
85         p.mNegativeButtonListener = this;
86 
87         notificationId = intent.getIntExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_NOTIF_ID, -1);
88         timeout = intent.getIntExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_TIMEOUT, default_response_timeout);
89         default_response = intent.getIntExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_DEFAULT_RESPONSE, GpsNetInitiatedHandler.GPS_NI_RESPONSE_ACCEPT);
90         if (DEBUG) Log.d(TAG, "onCreate() : notificationId: " + notificationId + " timeout: " + timeout + " default_response:" + default_response);
91 
92         mHandler.sendMessageDelayed(mHandler.obtainMessage(GPS_NO_RESPONSE_TIME_OUT), (timeout * 1000));
93         setupAlert();
94     }
95 
96     @Override
onResume()97     protected void onResume() {
98         super.onResume();
99         if (DEBUG) Log.d(TAG, "onResume");
100     }
101 
102     @Override
onPause()103     protected void onPause() {
104         super.onPause();
105         if (DEBUG) Log.d(TAG, "onPause");
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
onClick(DialogInterface dialog, int which)111     public void onClick(DialogInterface dialog, int which) {
112         if (which == POSITIVE_BUTTON) {
113             sendUserResponse(GpsNetInitiatedHandler.GPS_NI_RESPONSE_ACCEPT);
114         }
115         if (which == NEGATIVE_BUTTON) {
116             sendUserResponse(GpsNetInitiatedHandler.GPS_NI_RESPONSE_DENY);
117         }
118 
119         // No matter what, finish the activity
120         finish();
121         notificationId = -1;
122     }
123 
124     // Respond to NI Handler under GnssLocationProvider, 1 = accept, 2 = deny
sendUserResponse(int response)125     private void sendUserResponse(int response) {
126         if (DEBUG) Log.d(TAG, "sendUserResponse, response: " + response);
127         LocationManagerInternal lm = LocalServices.getService(LocationManagerInternal.class);
128         lm.sendNiResponse(notificationId, response);
129     }
130 }
131