• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.graphics.drawable.Icon;
22 import android.net.ConnectivityManager;
23 import com.android.internal.logging.MetricsProto.MetricsEvent;
24 import com.android.settings.R;
25 import com.android.settings.Settings;
26 import com.android.settingslib.WirelessUtils;
27 
28 public class AirplaneModeCondition extends Condition {
29 
AirplaneModeCondition(ConditionManager conditionManager)30     public AirplaneModeCondition(ConditionManager conditionManager) {
31         super(conditionManager);
32     }
33 
34     @Override
refreshState()35     public void refreshState() {
36         setActive(WirelessUtils.isAirplaneModeOn(mManager.getContext()));
37     }
38 
39     @Override
getReceiverClass()40     protected Class<?> getReceiverClass() {
41         return Receiver.class;
42     }
43 
44     @Override
getIcon()45     public Icon getIcon() {
46         return Icon.createWithResource(mManager.getContext(), R.drawable.ic_airplane);
47     }
48 
49     @Override
getTitle()50     public CharSequence getTitle() {
51         return mManager.getContext().getString(R.string.condition_airplane_title);
52     }
53 
54     @Override
getSummary()55     public CharSequence getSummary() {
56         return mManager.getContext().getString(R.string.condition_airplane_summary);
57     }
58 
59     @Override
getActions()60     public CharSequence[] getActions() {
61         return new CharSequence[] { mManager.getContext().getString(R.string.condition_turn_off) };
62     }
63 
64     @Override
onPrimaryClick()65     public void onPrimaryClick() {
66         mManager.getContext().startActivity(new Intent(mManager.getContext(),
67                 Settings.WirelessSettingsActivity.class));
68     }
69 
70     @Override
onActionClick(int index)71     public void onActionClick(int index) {
72         if (index == 0) {
73             ConnectivityManager.from(mManager.getContext()).setAirplaneMode(false);
74             setActive(false);
75         } else {
76             throw new IllegalArgumentException("Unexpected index " + index);
77         }
78     }
79 
80     @Override
getMetricsConstant()81     public int getMetricsConstant() {
82         return MetricsEvent.SETTINGS_CONDITION_AIRPLANE_MODE;
83     }
84 
85     public static class Receiver extends BroadcastReceiver {
86         @Override
onReceive(Context context, Intent intent)87         public void onReceive(Context context, Intent intent) {
88             if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
89                 ConditionManager.get(context).getCondition(AirplaneModeCondition.class)
90                         .refreshState();
91             }
92         }
93     }
94 }
95