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