PackageIntentsReceiver.java revision 2ee6716186c264fe56dcce5855ec5949ba307610
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.receiver;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.SharedPreferences;
23import android.net.Uri;
24import android.tv.TvInputInfo;
25import android.tv.TvInputManager;
26
27import com.android.tv.util.TvSettings;
28import com.android.tv.util.Utils;
29
30import java.util.HashSet;
31import java.util.List;
32import java.util.Set;
33
34/**
35 * A class for handling the broadcast intents from PackageManager.
36 */
37public class PackageIntentsReceiver extends BroadcastReceiver {
38    private TvInputManager mTvInputManager;
39    private SharedPreferences mPreferences;
40
41    @Override
42    public void onReceive(Context context, Intent intent) {
43        if (mPreferences == null) {
44            mPreferences = Utils.getSharedPreferencesOfDisplayNameForInput(context);
45        }
46        if (mTvInputManager == null) {
47            mTvInputManager = (TvInputManager) context.getSystemService(Context.TV_INPUT_SERVICE);
48        }
49
50        String action = intent.getAction();
51        if (Intent.ACTION_PACKAGE_REMOVED.equals(action)
52                && !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
53            Uri uri = intent.getData();
54            onPackageFullyRemoved(uri != null ? uri.getSchemeSpecificPart() : null);
55        }
56    }
57
58    private void onPackageFullyRemoved(String packageName) {
59        if (packageName == null || packageName.isEmpty()) {
60            return;
61        }
62        cleanupUnusedDisplayInputName();
63    }
64
65    private void cleanupUnusedDisplayInputName() {
66        Set<String> keys = mPreferences.getAll().keySet();
67        HashSet<String> unusedKeys = new HashSet<String>(keys);
68        for (String key : keys) {
69            if (!key.startsWith(TvSettings.PREF_DISPLAY_INPUT_NAME)) {
70                unusedKeys.remove(key);
71            }
72        }
73        List<TvInputInfo> inputs = mTvInputManager.getTvInputList();
74        for (TvInputInfo input : inputs) {
75            unusedKeys.remove(TvSettings.PREF_DISPLAY_INPUT_NAME + input.getId());
76        }
77        if (!unusedKeys.isEmpty()) {
78            SharedPreferences.Editor editor = mPreferences.edit();
79            for (String key : unusedKeys) {
80                editor.remove(key);
81            }
82            editor.commit();
83        }
84    }
85}
86