1/*
2 * Copyright (C) 2014 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
17package com.android.tv.settings.device.display.widi;
18
19import com.android.tv.settings.R;
20import com.android.tv.settings.dialog.old.Action;
21import com.android.tv.settings.dialog.old.ActionAdapter;
22import com.android.tv.settings.dialog.old.ActionFragment;
23import com.android.tv.settings.dialog.old.ContentFragment;
24import com.android.tv.settings.dialog.old.DialogActivity;
25
26import android.content.Context;
27import android.hardware.display.DisplayManager;
28import android.hardware.display.WifiDisplay;
29import android.hardware.display.WifiDisplayStatus;
30import android.os.Bundle;
31import android.provider.Settings;
32
33import java.util.ArrayList;
34
35/**
36 * Activity allowing the selection of wifi display settings.
37 */
38public class WifiDisplayActivity extends DialogActivity implements ActionAdapter.Listener {
39
40    enum Tag {
41        RENAME, WIFI_DISPLAY, PIN_REQUIRED, WIFI_DISPLAY_ON, WIFI_DISPLAY_OFF, PIN_REQUIRED_ON,
42        PIN_REQUIRED_OFF
43    }
44
45    private DisplayManager mDisplayManager;
46    private WifiDisplayStatus mWifiDisplayStatus;
47    private ActionFragment mActionFragment;
48
49    @Override
50    protected void onCreate(Bundle savedInstanceState) {
51        super.onCreate(savedInstanceState);
52
53        mDisplayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
54        mWifiDisplayStatus = mDisplayManager.getWifiDisplayStatus();
55
56        mActionFragment = ActionFragment.newInstance(getMainActions());
57        setContentAndActionFragments(ContentFragment.newInstance(
58                getString(R.string.accessories_wifi_display), null, null,
59                R.drawable.ic_settings_widi, getResources().getColor(R.color.icon_background)),
60                mActionFragment);
61    }
62
63    @Override
64    public void onActionClicked(Action action) {
65        Tag tag = Tag.valueOf(action.getKey());
66        switch (tag) {
67            case PIN_REQUIRED:
68                showPinRequired();
69                break;
70            case PIN_REQUIRED_OFF:
71                // TODO: disable pin requirement.
72                showMainScreen();
73                break;
74            case PIN_REQUIRED_ON:
75                // TODO: enable pin requirement.
76                showMainScreen();
77                break;
78            case RENAME:
79                showRename();
80                break;
81            case WIFI_DISPLAY:
82                showWifiDisplay();
83                break;
84            case WIFI_DISPLAY_OFF:
85                setWifiDisplayEnabled(false);
86                showMainScreen();
87                break;
88            case WIFI_DISPLAY_ON:
89                setWifiDisplayEnabled(true);
90                showMainScreen();
91                break;
92            default:
93                break;
94        }
95    }
96
97    private void showWifiDisplay() {
98        boolean isOn = getWifiDisplayEnabled();
99        ArrayList<Action> actions = new ArrayList<Action>();
100        actions.add(new Action.Builder().key(Tag.WIFI_DISPLAY_ON.name())
101                .title(getString(R.string.action_on_title)).checked(isOn).build());
102        actions.add(new Action.Builder().key(Tag.WIFI_DISPLAY_OFF.name())
103                .title(getString(R.string.action_off_title)).checked(!isOn).build());
104        setContentAndActionFragments(ContentFragment.newInstance(
105                getString(R.string.accessories_wifi_display_enable),
106                getString(R.string.accessories_wifi_display), null, R.drawable.ic_settings_widi,
107                getResources().getColor(R.color.icon_background)),
108                ActionFragment.newInstance(actions));
109    }
110
111    private void showRename() {
112        // TODO: launch multi-page form with current name pre-filled out in form.
113    }
114
115    private void showPinRequired() {
116        ArrayList<Action> actions = new ArrayList<Action>();
117        actions.add(new Action.Builder().key(Tag.PIN_REQUIRED_ON.name())
118                .title(getString(R.string.action_on_title)).checked(false).build());
119        actions.add(new Action.Builder().key(Tag.PIN_REQUIRED_OFF.name())
120                .title(getString(R.string.action_off_title)).checked(true).build());
121        setContentAndActionFragments(ContentFragment.newInstance(
122                getString(R.string.accessories_wifi_display_pin_required),
123                getString(R.string.accessories_wifi_display), null, R.drawable.ic_settings_widi,
124                getResources().getColor(R.color.icon_background)),
125                ActionFragment.newInstance(actions));
126    }
127
128    private void showMainScreen() {
129        ((ActionAdapter) mActionFragment.getAdapter()).setActions(getMainActions());
130        getFragmentManager().popBackStack(null, 0);
131    }
132
133    private ArrayList<Action> getMainActions() {
134        ArrayList<Action> actions = new ArrayList<Action>();
135        if (mWifiDisplayStatus.getFeatureState() != WifiDisplayStatus.FEATURE_STATE_DISABLED) {
136            actions.add(new Action.Builder().key(Tag.RENAME.name())
137                    .title(getString(R.string.accessories_wifi_display_rename_device)).build());
138            actions.add(new Action.Builder().key(Tag.WIFI_DISPLAY.name())
139                    .title(getString(R.string.accessories_wifi_display_enable))
140                    .description(getWifiDisplayEnabled() ? getString(R.string.action_on_title)
141                            : getString(R.string.action_off_title)).build());
142            actions.add(new Action.Builder().key(Tag.PIN_REQUIRED.name())
143                    .title(getString(R.string.accessories_wifi_display_pin_required))
144                    .description(getString(R.string.action_off_title)).build());
145        }
146        return actions;
147    }
148
149    private boolean getWifiDisplayEnabled() {
150        return Settings.Global.getInt(getContentResolver(), Settings.Global.WIFI_DISPLAY_ON, 0)
151                != 0;
152    }
153
154    private void setWifiDisplayEnabled(boolean enabled) {
155        Settings.Global.putInt(getContentResolver(), Settings.Global.WIFI_DISPLAY_ON,
156                enabled ? 1 : 0);
157    }
158}
159