Home | History | Annotate | Download | only in conditional
      1 /*
      2  * Copyright (C) 2015 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 package com.android.settings.dashboard.conditional;
     17 
     18 import android.content.BroadcastReceiver;
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.content.IntentFilter;
     22 import android.graphics.drawable.Icon;
     23 import android.net.ConnectivityManager;
     24 import android.util.Log;
     25 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
     26 import com.android.settings.R;
     27 import com.android.settings.Settings;
     28 import com.android.settingslib.WirelessUtils;
     29 
     30 public class AirplaneModeCondition extends Condition {
     31     public static String TAG = "APM_Condition";
     32 
     33     private final Receiver mReceiver;
     34 
     35     private static final IntentFilter AIRPLANE_MODE_FILTER =
     36         new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
     37 
     38     public AirplaneModeCondition(ConditionManager conditionManager) {
     39         super(conditionManager);
     40         mReceiver = new Receiver();
     41     }
     42 
     43     @Override
     44     public void refreshState() {
     45         Log.d(TAG, "APM condition refreshed");
     46         setActive(WirelessUtils.isAirplaneModeOn(mManager.getContext()));
     47     }
     48 
     49     @Override
     50     protected BroadcastReceiver getReceiver() {
     51         return mReceiver;
     52     }
     53 
     54     @Override
     55     protected IntentFilter getIntentFilter() {
     56         return AIRPLANE_MODE_FILTER;
     57     }
     58 
     59     @Override
     60     public Icon getIcon() {
     61         return Icon.createWithResource(mManager.getContext(), R.drawable.ic_airplane);
     62     }
     63 
     64     @Override
     65     protected void setActive(boolean active) {
     66         super.setActive(active);
     67         Log.d(TAG, "setActive was called with " + active);
     68     }
     69 
     70     @Override
     71     public CharSequence getTitle() {
     72         return mManager.getContext().getString(R.string.condition_airplane_title);
     73     }
     74 
     75     @Override
     76     public CharSequence getSummary() {
     77         return mManager.getContext().getString(R.string.condition_airplane_summary);
     78     }
     79 
     80     @Override
     81     public CharSequence[] getActions() {
     82         return new CharSequence[] { mManager.getContext().getString(R.string.condition_turn_off) };
     83     }
     84 
     85     @Override
     86     public void onPrimaryClick() {
     87         mManager.getContext().startActivity(new Intent(mManager.getContext(),
     88                 Settings.NetworkDashboardActivity.class));
     89     }
     90 
     91     @Override
     92     public void onActionClick(int index) {
     93         if (index == 0) {
     94             ConnectivityManager.from(mManager.getContext()).setAirplaneMode(false);
     95             setActive(false);
     96         } else {
     97             throw new IllegalArgumentException("Unexpected index " + index);
     98         }
     99     }
    100 
    101     @Override
    102     public int getMetricsConstant() {
    103         return MetricsEvent.SETTINGS_CONDITION_AIRPLANE_MODE;
    104     }
    105 
    106     public static class Receiver extends BroadcastReceiver {
    107         @Override
    108         public void onReceive(Context context, Intent intent) {
    109             if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
    110                 ConditionManager.get(context).getCondition(AirplaneModeCondition.class)
    111                         .refreshState();
    112             }
    113         }
    114     }
    115 }
    116