aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/customization/TvCustomizationManager.java
blob: ed6b98ca7eef83950f239629edaa9fc5ce1e994e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.customization;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntDef;
import android.text.TextUtils;
import android.util.Log;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TvCustomizationManager {
    private static final String TAG = "TvCustomizationManager";
    private static final boolean DEBUG = false;

    private static final String[] CUSTOMIZE_PERMISSIONS = {
            "com.android.tv.permission.CUSTOMIZE_TV_APP"
    };

    private static final String CATEGORY_TV_CUSTOMIZATION =
            "com.android.tv.category";

    /**
     * Row IDs to share customized actions.
     * Only rows listed below can have customized action.
     */
    public static final String ID_OPTIONS_ROW = "options_row";
    public static final String ID_PARTNER_ROW = "partner_row";

    @IntDef({TRICKPLAY_MODE_ENABLED, TRICKPLAY_MODE_DISABLED, TRICKPLAY_MODE_USE_EXTERNAL_STORAGE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface TRICKPLAY_MODE {}
    public static final int TRICKPLAY_MODE_ENABLED = 0;
    public static final int TRICKPLAY_MODE_DISABLED = 1;
    public static final int TRICKPLAY_MODE_USE_EXTERNAL_STORAGE = 2;

    private static final String[] TRICKPLAY_MODE_STRINGS = {
        "enabled",
        "disabled",
        "use_external_storage_only"
    };

    private static final HashMap<String, String> INTENT_CATEGORY_TO_ROW_ID;
    static {
        INTENT_CATEGORY_TO_ROW_ID = new HashMap<>();
        INTENT_CATEGORY_TO_ROW_ID.put(CATEGORY_TV_CUSTOMIZATION + ".OPTIONS_ROW", ID_OPTIONS_ROW);
        INTENT_CATEGORY_TO_ROW_ID.put(CATEGORY_TV_CUSTOMIZATION + ".PARTNER_ROW", ID_PARTNER_ROW);
    }

    private static final String RES_ID_PARTNER_ROW_TITLE = "partner_row_title";
    private static final String RES_ID_HAS_LINUX_DVB_BUILT_IN_TUNER =
            "has_linux_dvb_built_in_tuner";
    private static final String RES_ID_TRICKPLAY_MODE = "trickplay_mode";

    private static final String RES_TYPE_STRING = "string";
    private static final String RES_TYPE_BOOLEAN = "bool";

    private static String sCustomizationPackage;
    private static Boolean sHasLinuxDvbBuiltInTuner;
    private static @TRICKPLAY_MODE Integer sTrickplayMode;

    private final Context mContext;
    private boolean mInitialized;

    private String mPartnerRowTitle;
    private final Map<String, List<CustomAction>> mRowIdToCustomActionsMap = new HashMap<>();

    public TvCustomizationManager(Context context) {
        mContext = context;
        mInitialized = false;
    }

    /**
     * Returns {@code true} if there's a customization package installed and it specifies built-in
     * tuner devices are available. The built-in tuner should support DVB API to be recognized by
     * Live TV.
     */
    public static boolean hasLinuxDvbBuiltInTuner(Context context) {
        if (sHasLinuxDvbBuiltInTuner == null) {
            if (TextUtils.isEmpty(getCustomizationPackageName(context))) {
                sHasLinuxDvbBuiltInTuner = false;
            } else {
                try {
                    Resources res = context.getPackageManager()
                            .getResourcesForApplication(sCustomizationPackage);
                    int resId = res.getIdentifier(RES_ID_HAS_LINUX_DVB_BUILT_IN_TUNER,
                            RES_TYPE_BOOLEAN, sCustomizationPackage);
                    sHasLinuxDvbBuiltInTuner = resId != 0 && res.getBoolean(resId);
                } catch (NameNotFoundException e) {
                    sHasLinuxDvbBuiltInTuner = false;
                }
            }
        }
        return sHasLinuxDvbBuiltInTuner;
    }

    public static @TRICKPLAY_MODE int getTrickplayMode(Context context) {
        if (sTrickplayMode == null) {
            if (TextUtils.isEmpty(getCustomizationPackageName(context))) {
                sTrickplayMode = TRICKPLAY_MODE_ENABLED;
            } else {
                try {
                    String customization = null;
                    Resources res = context.getPackageManager()
                            .getResourcesForApplication(sCustomizationPackage);
                    int resId = res.getIdentifier(RES_ID_TRICKPLAY_MODE,
                            RES_TYPE_STRING, sCustomizationPackage);
                    customization = resId == 0 ? null : res.getString(resId);
                    sTrickplayMode = TRICKPLAY_MODE_ENABLED;
                    if (customization != null) {
                        for (int i = 0; i < TRICKPLAY_MODE_STRINGS.length; ++i) {
                            if (TRICKPLAY_MODE_STRINGS[i].equalsIgnoreCase(customization)) {
                                sTrickplayMode = i;
                                break;
                            }
                        }
                    }
                } catch (NameNotFoundException e) {
                    sTrickplayMode = TRICKPLAY_MODE_ENABLED;
                }
            }
        }
        return sTrickplayMode;
    }

    private static String getCustomizationPackageName(Context context) {
        if (sCustomizationPackage == null) {
            List<PackageInfo> packageInfos = context.getPackageManager()
                    .getPackagesHoldingPermissions(CUSTOMIZE_PERMISSIONS, 0);
            sCustomizationPackage = packageInfos.size() == 0 ? "" : packageInfos.get(0).packageName;
        }
        return sCustomizationPackage;
    }

    /**
     * Initialize TV customization options.
     * Run this API only on the main thread.
     */
    public void initialize() {
        if (mInitialized) {
            return;
        }
        mInitialized = true;
        if (!TextUtils.isEmpty(getCustomizationPackageName(mContext))) {
            buildCustomActions();
            buildPartnerRow();
        }
    }

    private void buildCustomActions() {
        mRowIdToCustomActionsMap.clear();
        PackageManager pm = mContext.getPackageManager();
        for (String intentCategory : INTENT_CATEGORY_TO_ROW_ID.keySet()) {
            Intent customOptionIntent = new Intent(Intent.ACTION_MAIN);
            customOptionIntent.addCategory(intentCategory);

            List<ResolveInfo> activities = pm.queryIntentActivities(customOptionIntent,
                    PackageManager.GET_RECEIVERS | PackageManager.GET_RESOLVED_FILTER
                            | PackageManager.GET_META_DATA);
            for (ResolveInfo info : activities) {
                String packageName = info.activityInfo.packageName;
                if (!TextUtils.equals(packageName, sCustomizationPackage)) {
                    Log.w(TAG, "A customization package " + sCustomizationPackage
                            + " already exist. Ignoring " + packageName);
                    continue;
                }

                int position = info.filter.getPriority();
                String title = info.loadLabel(pm).toString();
                Drawable drawable = info.loadIcon(pm);
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(intentCategory);
                intent.setClassName(sCustomizationPackage, info.activityInfo.name);

                String rowId = INTENT_CATEGORY_TO_ROW_ID.get(intentCategory);
                List<CustomAction> actions = mRowIdToCustomActionsMap.get(rowId);
                if (actions == null) {
                    actions = new ArrayList<>();
                    mRowIdToCustomActionsMap.put(rowId, actions);
                }
                actions.add(new CustomAction(position, title, drawable, intent));
            }
        }
        // Sort items by position
        for (List<CustomAction> actions : mRowIdToCustomActionsMap.values()) {
            Collections.sort(actions);
        }

        if (DEBUG) {
            Log.d(TAG, "Dumping custom actions");
            for (String id : mRowIdToCustomActionsMap.keySet()) {
                for (CustomAction action : mRowIdToCustomActionsMap.get(id)) {
                    Log.d(TAG, "Custom row rowId=" + id + " title=" + action.getTitle()
                        + " class=" + action.getIntent());
                }
            }
            Log.d(TAG, "Dumping custom actions - end of dump");
        }
    }

    /**
     * Returns custom actions for given row id.
     *
     * Row ID is one of ID_OPTIONS_ROW or ID_PARTNER_ROW.
     */
    public List<CustomAction> getCustomActions(String rowId) {
        return mRowIdToCustomActionsMap.get(rowId);
    }

    private void buildPartnerRow() {
        mPartnerRowTitle = null;
        Resources res;
        try {
            res = mContext.getPackageManager()
                    .getResourcesForApplication(sCustomizationPackage);
        } catch (NameNotFoundException e) {
            Log.w(TAG, "Could not get resources for package " + sCustomizationPackage);
            return;
        }
        int resId = res.getIdentifier(
                RES_ID_PARTNER_ROW_TITLE, RES_TYPE_STRING, sCustomizationPackage);
        if (resId != 0) {
            mPartnerRowTitle = res.getString(resId);
        }
        if (DEBUG) Log.d(TAG, "Partner row title [" + mPartnerRowTitle + "]");
    }

    /**
     * Returns partner row title.
     */
    public String getPartnerRowTitle() {
        return mPartnerRowTitle;
    }
}