Home | History | Annotate | Download | only in app
      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.Message;
     28 import android.widget.Toast;
     29 import android.util.Log;
     30 import android.location.LocationManager;
     31 
     32 import com.android.internal.R;
     33 import com.android.internal.location.GpsNetInitiatedHandler;
     34 
     35 /**
     36  * This activity is shown to the user for him/her 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     private static final boolean VERBOSE = false;
     45 
     46     private static final int POSITIVE_BUTTON = AlertDialog.BUTTON_POSITIVE;
     47     private static final int NEGATIVE_BUTTON = AlertDialog.BUTTON_NEGATIVE;
     48 
     49     private static final int GPS_NO_RESPONSE_TIME_OUT = 1;
     50     // Received ID from intent, -1 when no notification is in progress
     51     private int notificationId = -1;
     52     private int timeout = -1;
     53     private int default_response = -1;
     54     private int default_response_timeout = 6;
     55 
     56     /** Used to detect when NI request is received */
     57     private BroadcastReceiver mNetInitiatedReceiver = new BroadcastReceiver() {
     58         @Override
     59         public void onReceive(Context context, Intent intent) {
     60             if (DEBUG) Log.d(TAG, "NetInitiatedReceiver onReceive: " + intent.getAction());
     61             if (intent.getAction() == GpsNetInitiatedHandler.ACTION_NI_VERIFY) {
     62                 handleNIVerify(intent);
     63             }
     64         }
     65     };
     66 
     67     private final Handler mHandler = new Handler() {
     68         public void handleMessage(Message msg) {
     69             switch (msg.what) {
     70             case GPS_NO_RESPONSE_TIME_OUT: {
     71                 if (notificationId != -1) {
     72                     sendUserResponse(default_response);
     73                 }
     74                 finish();
     75             }
     76             break;
     77             default:
     78             }
     79         }
     80     };
     81 
     82     @Override
     83     protected void onCreate(Bundle savedInstanceState) {
     84         super.onCreate(savedInstanceState);
     85 
     86         // Set up the "dialog"
     87         final Intent intent = getIntent();
     88         final AlertController.AlertParams p = mAlertParams;
     89         Context context = getApplicationContext();
     90         p.mIconId = com.android.internal.R.drawable.ic_dialog_usb;
     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
    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 
    137     // Respond to NI Handler under GpsLocationProvider, 1 = accept, 2 = deny
    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     private void handleNIVerify(Intent intent) {
    146         int notifId = intent.getIntExtra(GpsNetInitiatedHandler.NI_INTENT_KEY_NOTIF_ID, -1);
    147         notificationId = notifId;
    148 
    149         if (DEBUG) Log.d(TAG, "handleNIVerify action: " + intent.getAction());
    150     }
    151 
    152     private void showNIError() {
    153         Toast.makeText(this, "NI error" /* com.android.internal.R.string.usb_storage_error_message */,
    154                 Toast.LENGTH_LONG).show();
    155     }
    156 }
    157