blob: 89aa770d7f1c52ad0d7c2a0e8c36defa8e04c92a [file] [log] [blame]
Danke Xie22d1f9f2009-08-18 18:28:45 -04001/*
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
17package com.android.internal.app;
18
19import android.app.AlertDialog;
Artur Satayeved5a6ae2019-12-10 17:47:54 +000020import android.compat.annotation.UnsupportedAppUsage;
Danke Xie22d1f9f2009-08-18 18:28:45 -040021import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.content.Intent;
25import android.content.IntentFilter;
Artur Satayeved5a6ae2019-12-10 17:47:54 +000026import android.location.LocationManager;
Danke Xie22d1f9f2009-08-18 18:28:45 -040027import android.os.Bundle;
Alexy Shin7adcdec2011-07-18 09:42:55 -070028import android.os.Handler;
29import android.os.Message;
Danke Xie22d1f9f2009-08-18 18:28:45 -040030import android.util.Log;
Artur Satayeved5a6ae2019-12-10 17:47:54 +000031import android.widget.Toast;
Hakan Gustavssondea74b02010-11-22 16:08:35 +010032
33import com.android.internal.R;
Danke Xie22d1f9f2009-08-18 18:28:45 -040034import 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 */
40public 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
Christian Mehlmauer746a95a2010-05-17 21:16:20 +020047 private static final int POSITIVE_BUTTON = AlertDialog.BUTTON_POSITIVE;
48 private static final int NEGATIVE_BUTTON = AlertDialog.BUTTON_NEGATIVE;
Danke Xie22d1f9f2009-08-18 18:28:45 -040049
Alexy Shin7adcdec2011-07-18 09:42:55 -070050 private static final int GPS_NO_RESPONSE_TIME_OUT = 1;
Danke Xie22d1f9f2009-08-18 18:28:45 -040051 // Received ID from intent, -1 when no notification is in progress
52 private int notificationId = -1;
Alexy Shin7adcdec2011-07-18 09:42:55 -070053 private int timeout = -1;
54 private int default_response = -1;
55 private int default_response_timeout = 6;
Danke Xie22d1f9f2009-08-18 18:28:45 -040056
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
Alexy Shin7adcdec2011-07-18 09:42:55 -070068 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
Danke Xie22d1f9f2009-08-18 18:28:45 -040083 @Override
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;
Hakan Gustavssondea74b02010-11-22 16:08:35 +010090 Context context = getApplicationContext();
Danke Xie22d1f9f2009-08-18 18:28:45 -040091 p.mTitle = intent.getStringExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_TITLE);
92 p.mMessage = intent.getStringExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_MESSAGE);
Hakan Gustavssondea74b02010-11-22 16:08:35 +010093 p.mPositiveButtonText = String.format(context.getString(R.string.gpsVerifYes));
Danke Xie22d1f9f2009-08-18 18:28:45 -040094 p.mPositiveButtonListener = this;
Hakan Gustavssondea74b02010-11-22 16:08:35 +010095 p.mNegativeButtonText = String.format(context.getString(R.string.gpsVerifNo));
Danke Xie22d1f9f2009-08-18 18:28:45 -040096 p.mNegativeButtonListener = this;
97
98 notificationId = intent.getIntExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_NOTIF_ID, -1);
Alexy Shin7adcdec2011-07-18 09:42:55 -070099 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);
Danke Xie22d1f9f2009-08-18 18:28:45 -0400102
Alexy Shin7adcdec2011-07-18 09:42:55 -0700103 mHandler.sendMessageDelayed(mHandler.obtainMessage(GPS_NO_RESPONSE_TIME_OUT), (timeout * 1000));
Danke Xie22d1f9f2009-08-18 18:28:45 -0400104 setupAlert();
105 }
106
107 @Override
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
115 protected void onPause() {
116 super.onPause();
117 if (DEBUG) Log.d(TAG, "onPause");
118 unregisterReceiver(mNetInitiatedReceiver);
119 }
120
121 /**
122 * {@inheritDoc}
123 */
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
Lifu Tang30f95a72016-01-07 23:20:38 -0800137 // Respond to NI Handler under GnssLocationProvider, 1 = accept, 2 = deny
Danke Xie22d1f9f2009-08-18 18:28:45 -0400138 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
Andrei Onea15884392019-03-22 17:28:11 +0000145 @UnsupportedAppUsage
Danke Xie22d1f9f2009-08-18 18:28:45 -0400146 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
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}