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