Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      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.systemui.net;
     18 
     19 import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
     20 import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
     21 import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
     22 import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
     23 
     24 import android.app.Activity;
     25 import android.app.AlertDialog;
     26 import android.app.Dialog;
     27 import android.content.Context;
     28 import android.content.DialogInterface;
     29 import android.net.INetworkPolicyManager;
     30 import android.net.NetworkPolicy;
     31 import android.net.NetworkTemplate;
     32 import android.os.Bundle;
     33 import android.os.RemoteException;
     34 import android.os.ServiceManager;
     35 import android.util.Log;
     36 import android.view.WindowManager;
     37 
     38 import com.android.systemui.R;
     39 
     40 /**
     41  * Notify user that a {@link NetworkTemplate} is over its
     42  * {@link NetworkPolicy#limitBytes}, giving them the choice of acknowledging or
     43  * "snoozing" the limit.
     44  */
     45 public class NetworkOverLimitActivity extends Activity {
     46     private static final String TAG = "NetworkOverLimitActivity";
     47 
     48     @Override
     49     public void onCreate(Bundle icicle) {
     50         super.onCreate(icicle);
     51 
     52         final NetworkTemplate template = getIntent().getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
     53         final AlertDialog.Builder builder = new AlertDialog.Builder(this);
     54         builder.setTitle(getLimitedDialogTitleForTemplate(template));
     55         builder.setMessage(R.string.data_usage_disabled_dialog);
     56 
     57         builder.setPositiveButton(android.R.string.ok, null);
     58         builder.setNegativeButton(
     59                 R.string.data_usage_disabled_dialog_enable, new DialogInterface.OnClickListener() {
     60                     public void onClick(DialogInterface dialog, int which) {
     61                         snoozePolicy(template);
     62                     }
     63                 });
     64 
     65         final Dialog dialog = builder.create();
     66         dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
     67         dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
     68             public void onDismiss(DialogInterface dialog) {
     69                 finish();
     70             }
     71         });
     72 
     73         dialog.show();
     74     }
     75 
     76     private void snoozePolicy(NetworkTemplate template) {
     77         final INetworkPolicyManager policyService = INetworkPolicyManager.Stub.asInterface(
     78                 ServiceManager.getService(Context.NETWORK_POLICY_SERVICE));
     79         try {
     80             policyService.snoozeLimit(template);
     81         } catch (RemoteException e) {
     82             Log.w(TAG, "problem snoozing network policy", e);
     83         }
     84     }
     85 
     86     private static int getLimitedDialogTitleForTemplate(NetworkTemplate template) {
     87         switch (template.getMatchRule()) {
     88             case MATCH_MOBILE_3G_LOWER:
     89                 return R.string.data_usage_disabled_dialog_3g_title;
     90             case MATCH_MOBILE_4G:
     91                 return R.string.data_usage_disabled_dialog_4g_title;
     92             case MATCH_MOBILE_ALL:
     93                 return R.string.data_usage_disabled_dialog_mobile_title;
     94             default:
     95                 return R.string.data_usage_disabled_dialog_title;
     96         }
     97     }
     98 }
     99