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 android.app.AlertDialog;
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.location.LocationManager;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.os.Message;
30 import android.util.Log;
31 import android.widget.Toast;
32 
33 import com.android.internal.R;
34 import com.android.internal.location.GpsNetInitiatedHandler;
35 
36 /**
37  * This activity is shown to the user for him/her to accept or deny network-initiated
38  * requests. It uses the alert dialog style. It will be launched from a notification.
39  */
40 public class NetInitiatedActivity extends AlertActivity implements DialogInterface.OnClickListener {
41 
42     private static final String TAG = "NetInitiatedActivity";
43 
44     private static final boolean DEBUG = true;
45     private static final boolean VERBOSE = false;
46 
47     private static final int POSITIVE_BUTTON = AlertDialog.BUTTON_POSITIVE;
48     private static final int NEGATIVE_BUTTON = AlertDialog.BUTTON_NEGATIVE;
49 
50     private static final int GPS_NO_RESPONSE_TIME_OUT = 1;
51     // Received ID from intent, -1 when no notification is in progress
52     private int notificationId = -1;
53     private int timeout = -1;
54     private int default_response = -1;
55     private int default_response_timeout = 6;
56 
57     /** Used to detect when NI request is received */
58     private BroadcastReceiver mNetInitiatedReceiver = new BroadcastReceiver() {
59         @Override
60         public void onReceive(Context context, Intent intent) {
61             if (DEBUG) Log.d(TAG, "NetInitiatedReceiver onReceive: " + intent.getAction());
62             if (intent.getAction() == GpsNetInitiatedHandler.ACTION_NI_VERIFY) {
63                 handleNIVerify(intent);
64             }
65         }
66     };
67 
68     private final Handler mHandler = new Handler() {
69         public void handleMessage(Message msg) {
70             switch (msg.what) {
71             case GPS_NO_RESPONSE_TIME_OUT: {
72                 if (notificationId != -1) {
73                     sendUserResponse(default_response);
74                 }
75                 finish();
76             }
77             break;
78             default:
79             }
80         }
81     };
82 
83     @Override
onCreate(Bundle savedInstanceState)84     protected void onCreate(Bundle savedInstanceState) {
85         super.onCreate(savedInstanceState);
86 
87         // Set up the "dialog"
88         final Intent intent = getIntent();
89         final AlertController.AlertParams p = mAlertParams;
90         Context context = getApplicationContext();
91         p.mTitle = intent.getStringExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_TITLE);
92         p.mMessage = intent.getStringExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_MESSAGE);
93         p.mPositiveButtonText = String.format(context.getString(R.string.gpsVerifYes));
94         p.mPositiveButtonListener = this;
95         p.mNegativeButtonText = String.format(context.getString(R.string.gpsVerifNo));
96         p.mNegativeButtonListener = this;
97 
98         notificationId = intent.getIntExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_NOTIF_ID, -1);
99         timeout = intent.getIntExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_TIMEOUT, default_response_timeout);
100         default_response = intent.getIntExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_DEFAULT_RESPONSE, GpsNetInitiatedHandler.GPS_NI_RESPONSE_ACCEPT);
101         if (DEBUG) Log.d(TAG, "onCreate() : notificationId: " + notificationId + " timeout: " + timeout + " default_response:" + default_response);
102 
103         mHandler.sendMessageDelayed(mHandler.obtainMessage(GPS_NO_RESPONSE_TIME_OUT), (timeout * 1000));
104         setupAlert();
105     }
106 
107     @Override
onResume()108     protected void onResume() {
109         super.onResume();
110         if (DEBUG) Log.d(TAG, "onResume");
111         registerReceiver(mNetInitiatedReceiver, new IntentFilter(GpsNetInitiatedHandler.ACTION_NI_VERIFY));
112     }
113 
114     @Override
onPause()115     protected void onPause() {
116         super.onPause();
117         if (DEBUG) Log.d(TAG, "onPause");
118         unregisterReceiver(mNetInitiatedReceiver);
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
onClick(DialogInterface dialog, int which)124     public void onClick(DialogInterface dialog, int which) {
125         if (which == POSITIVE_BUTTON) {
126             sendUserResponse(GpsNetInitiatedHandler.GPS_NI_RESPONSE_ACCEPT);
127         }
128         if (which == NEGATIVE_BUTTON) {
129             sendUserResponse(GpsNetInitiatedHandler.GPS_NI_RESPONSE_DENY);
130         }
131 
132         // No matter what, finish the activity
133         finish();
134         notificationId = -1;
135     }
136 
137     // Respond to NI Handler under GnssLocationProvider, 1 = accept, 2 = deny
sendUserResponse(int response)138     private void sendUserResponse(int response) {
139         if (DEBUG) Log.d(TAG, "sendUserResponse, response: " + response);
140         LocationManager locationManager = (LocationManager)
141             this.getSystemService(Context.LOCATION_SERVICE);
142         locationManager.sendNiResponse(notificationId, response);
143     }
144 
145     @UnsupportedAppUsage
handleNIVerify(Intent intent)146     private void handleNIVerify(Intent intent) {
147         int notifId = intent.getIntExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_NOTIF_ID, -1);
148         notificationId = notifId;
149 
150         if (DEBUG) Log.d(TAG, "handleNIVerify action: " + intent.getAction());
151     }
152 
showNIError()153     private void showNIError() {
154         Toast.makeText(this, "NI error" /* com.android.internal.R.string.usb_storage_error_message */,
155                 Toast.LENGTH_LONG).show();
156     }
157 }
158