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