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.apps;
18
19import com.android.tv.settings.ActionBehavior;
20import com.android.tv.settings.ActionKey;
21import com.android.tv.settings.R;
22import com.android.tv.settings.dialog.old.Action;
23
24import android.content.res.Resources;
25
26import java.util.ArrayList;
27
28/**
29 * The different possible action types (screens).
30 */
31enum ActionType {
32    OPEN(new ActionBehavior[] { ActionBehavior.INIT },
33            R.string.device_apps_app_management_open),
34    FORCE_STOP(new ActionBehavior[] { ActionBehavior.INIT, ActionBehavior.OK,
35            ActionBehavior.CANCEL },
36            R.string.device_apps_app_management_force_stop,
37            R.string.device_apps_app_management_force_stop_desc),
38    UNINSTALL(new ActionBehavior[] { ActionBehavior.INIT, ActionBehavior.OK,
39            ActionBehavior.CANCEL },
40            R.string.device_apps_app_management_uninstall,
41            R.string.device_apps_app_management_uninstall_desc),
42    CLEAR_DATA(new ActionBehavior[] { ActionBehavior.INIT, ActionBehavior.OK,
43            ActionBehavior.CANCEL },
44            R.string.device_apps_app_management_clear_data,
45            R.string.device_apps_app_management_clear_data_desc,
46            R.string.device_apps_app_management_clear_data_what),
47    CLEAR_DEFAULTS(new ActionBehavior[] { ActionBehavior.INIT, ActionBehavior.OK,
48            ActionBehavior.CANCEL },
49            R.string.device_apps_app_management_clear_default),
50    CLEAR_CACHE(new ActionBehavior[] { ActionBehavior.INIT, ActionBehavior.OK,
51            ActionBehavior.CANCEL },
52            R.string.device_apps_app_management_clear_cache),
53    NOTIFICATIONS(new ActionBehavior[] { ActionBehavior.INIT, ActionBehavior.ON,
54            ActionBehavior.OFF },
55            R.string.device_apps_app_management_notifications),
56    PERMISSIONS(new ActionBehavior[] { ActionBehavior.INIT },
57            R.string.device_apps_app_management_permissions);
58
59    private final ActionBehavior[] mBehaviors;
60    private final int mNameResource;
61    private final int mDescResource;
62    private final int mDesc2Resource;
63
64    private ActionType(ActionBehavior[] behaviors, int nameResource) {
65        this(behaviors, nameResource, 0, 0);
66    }
67
68    private ActionType(ActionBehavior[] behaviors, int nameResource, int descResource) {
69        this(behaviors, nameResource, descResource, 0);
70    }
71
72    private ActionType(
73            ActionBehavior[] behaviors, int nameResource, int descResource, int desc2Resource) {
74        mBehaviors = behaviors;
75        mNameResource = nameResource;
76        mDescResource = descResource;
77        mDesc2Resource = desc2Resource;
78    }
79
80    String getDesc(Resources resources) {
81        return (mDescResource == 0) ? null : resources.getString(mDescResource);
82    }
83
84    String getDesc2(Resources resources) {
85        return (mDesc2Resource == 0) ? null : resources.getString(mDesc2Resource);
86    }
87
88    Action toInitAction(Resources resources) {
89        return toInitAction(resources, null);
90    }
91
92    Action toInitAction(Resources resources, String description) {
93        return toAction(resources, ActionBehavior.INIT, description);
94    }
95
96    Action toAction(Resources resources, ActionBehavior actionBehavior, String description) {
97        return new Action.Builder()
98                .key(new ActionKey<ActionType, ActionBehavior>(this, actionBehavior).getKey())
99                .title(resources.getString(mNameResource))
100                .description(description)
101                .build();
102    }
103
104    ArrayList<Action> toActions(Resources resources) {
105        ArrayList<Action> actions = new ArrayList<Action>();
106        for (ActionBehavior behavior : mBehaviors) {
107            if (behavior != ActionBehavior.INIT) {
108                actions.add(behavior.toAction(
109                        new ActionKey<ActionType, ActionBehavior>(this, behavior).getKey(),
110                        resources));
111            }
112        }
113        return actions;
114    }
115
116    ArrayList<Action> toSelectableActions(Resources resources, ActionBehavior selectedBehavior) {
117        ArrayList<Action> actions = new ArrayList<Action>();
118        for (ActionBehavior behavior : mBehaviors) {
119            if (behavior != ActionBehavior.INIT) {
120                actions.add(behavior.toAction(
121                        new ActionKey<ActionType, ActionBehavior>(this, behavior).getKey(),
122                        resources, (selectedBehavior == behavior)));
123            }
124        }
125        return actions;
126    }
127}
128