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 */
16package com.android.settings.dashboard.conditional;
17
18import android.content.BroadcastReceiver;
19import android.content.Context;
20import android.content.Intent;
21import android.graphics.drawable.Icon;
22import android.net.ConnectivityManager;
23import com.android.internal.logging.MetricsProto.MetricsEvent;
24import com.android.settings.R;
25import com.android.settings.Settings;
26import com.android.settingslib.WirelessUtils;
27
28public class AirplaneModeCondition extends Condition {
29
30    public AirplaneModeCondition(ConditionManager conditionManager) {
31        super(conditionManager);
32    }
33
34    @Override
35    public void refreshState() {
36        setActive(WirelessUtils.isAirplaneModeOn(mManager.getContext()));
37    }
38
39    @Override
40    protected Class<?> getReceiverClass() {
41        return Receiver.class;
42    }
43
44    @Override
45    public Icon getIcon() {
46        return Icon.createWithResource(mManager.getContext(), R.drawable.ic_airplane);
47    }
48
49    @Override
50    public CharSequence getTitle() {
51        return mManager.getContext().getString(R.string.condition_airplane_title);
52    }
53
54    @Override
55    public CharSequence getSummary() {
56        return mManager.getContext().getString(R.string.condition_airplane_summary);
57    }
58
59    @Override
60    public CharSequence[] getActions() {
61        return new CharSequence[] { mManager.getContext().getString(R.string.condition_turn_off) };
62    }
63
64    @Override
65    public void onPrimaryClick() {
66        mManager.getContext().startActivity(new Intent(mManager.getContext(),
67                Settings.WirelessSettingsActivity.class));
68    }
69
70    @Override
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
81    public int getMetricsConstant() {
82        return MetricsEvent.SETTINGS_CONDITION_AIRPLANE_MODE;
83    }
84
85    public static class Receiver extends BroadcastReceiver {
86        @Override
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