diff options
Diffstat (limited to 'src/com/android/tv/util/SetupUtils.java')
-rw-r--r-- | src/com/android/tv/util/SetupUtils.java | 245 |
1 files changed, 123 insertions, 122 deletions
diff --git a/src/com/android/tv/util/SetupUtils.java b/src/com/android/tv/util/SetupUtils.java index 32e3a81f..0d536320 100644 --- a/src/com/android/tv/util/SetupUtils.java +++ b/src/com/android/tv/util/SetupUtils.java @@ -28,24 +28,20 @@ import android.media.tv.TvInputManager; import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.annotation.UiThread; +import android.support.annotation.VisibleForTesting; import android.text.TextUtils; import android.util.ArraySet; import android.util.Log; - -import com.android.tv.ApplicationSingletons; -import com.android.tv.TvApplication; +import com.android.tv.TvSingletons; +import com.android.tv.common.BaseApplication; import com.android.tv.common.SoftPreconditions; -import com.android.tv.data.Channel; import com.android.tv.data.ChannelDataManager; -import com.android.tv.tuner.tvinput.TunerTvInputService; - +import com.android.tv.data.api.Channel; import java.util.Collections; import java.util.HashSet; import java.util.Set; -/** - * A utility class related to input setup. - */ +/** A utility class related to input setup. */ public class SetupUtils { private static final String TAG = "SetupUtils"; private static final boolean DEBUG = false; @@ -58,9 +54,8 @@ public class SetupUtils { // Recognized inputs means that the user already knows the inputs are installed. private static final String PREF_KEY_RECOGNIZED_INPUTS = "recognized_inputs"; private static final String PREF_KEY_IS_FIRST_TUNE = "is_first_tune"; - private static SetupUtils sSetupUtils; - private final TvApplication mTvApplication; + private final Context mContext; private final SharedPreferences mSharedPreferences; private final Set<String> mKnownInputs; private final Set<String> mSetUpInputs; @@ -68,106 +63,104 @@ public class SetupUtils { private boolean mIsFirstTune; private final String mTunerInputId; - private SetupUtils(TvApplication tvApplication) { - mTvApplication = tvApplication; - mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(tvApplication); + @VisibleForTesting + protected SetupUtils(Context context) { + mContext = context; + mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); mSetUpInputs = new ArraySet<>(); - mSetUpInputs.addAll(mSharedPreferences.getStringSet(PREF_KEY_SET_UP_INPUTS, - Collections.emptySet())); + mSetUpInputs.addAll( + mSharedPreferences.getStringSet(PREF_KEY_SET_UP_INPUTS, Collections.emptySet())); mKnownInputs = new ArraySet<>(); - mKnownInputs.addAll(mSharedPreferences.getStringSet(PREF_KEY_KNOWN_INPUTS, - Collections.emptySet())); + mKnownInputs.addAll( + mSharedPreferences.getStringSet(PREF_KEY_KNOWN_INPUTS, Collections.emptySet())); mRecognizedInputs = new ArraySet<>(); - mRecognizedInputs.addAll(mSharedPreferences.getStringSet(PREF_KEY_RECOGNIZED_INPUTS, - mKnownInputs)); + mRecognizedInputs.addAll( + mSharedPreferences.getStringSet(PREF_KEY_RECOGNIZED_INPUTS, mKnownInputs)); mIsFirstTune = mSharedPreferences.getBoolean(PREF_KEY_IS_FIRST_TUNE, true); - mTunerInputId = TvContract.buildInputId(new ComponentName(tvApplication, - TunerTvInputService.class)); + mTunerInputId = BaseApplication.getSingletons(context).getEmbeddedTunerInputId(); } /** - * Gets an instance of {@link SetupUtils}. + * Creates an instance of {@link SetupUtils}. + * + * <p><b>WARNING</b> this should only be called by the top level application. */ - public static SetupUtils getInstance(Context context) { - if (sSetupUtils != null) { - return sSetupUtils; - } - sSetupUtils = new SetupUtils((TvApplication) context.getApplicationContext()); - return sSetupUtils; + public static SetupUtils createForTvSingletons(Context context) { + return new SetupUtils(context.getApplicationContext()); } - /** - * Additional work after the setup of TV input. - */ - public void onTvInputSetupFinished(final String inputId, - @Nullable final Runnable postRunnable) { + /** Additional work after the setup of TV input. */ + public void onTvInputSetupFinished( + final String inputId, @Nullable final Runnable postRunnable) { // When TIS adds several channels, ChannelDataManager.Listener.onChannelList // Updated() can be called several times. In this case, it is hard to detect // which one is the last callback. To reduce error prune, we update channel // list again and make all channels of {@code inputId} browsable. onSetupDone(inputId); - final ChannelDataManager manager = mTvApplication.getChannelDataManager(); + final ChannelDataManager manager = + TvSingletons.getSingletons(mContext).getChannelDataManager(); if (!manager.isDbLoadFinished()) { - manager.addListener(new ChannelDataManager.Listener() { - @Override - public void onLoadFinished() { - manager.removeListener(this); - updateChannelsAfterSetup(mTvApplication, inputId, postRunnable); - } + manager.addListener( + new ChannelDataManager.Listener() { + @Override + public void onLoadFinished() { + manager.removeListener(this); + updateChannelsAfterSetup(mContext, inputId, postRunnable); + } - @Override - public void onChannelListUpdated() { } + @Override + public void onChannelListUpdated() {} - @Override - public void onChannelBrowsableChanged() { } - }); + @Override + public void onChannelBrowsableChanged() {} + }); } else { - updateChannelsAfterSetup(mTvApplication, inputId, postRunnable); + updateChannelsAfterSetup(mContext, inputId, postRunnable); } } - private static void updateChannelsAfterSetup(Context context, final String inputId, - final Runnable postRunnable) { - ApplicationSingletons appSingletons = TvApplication.getSingletons(context); - final ChannelDataManager manager = appSingletons.getChannelDataManager(); - manager.updateChannels(new Runnable() { - @Override - public void run() { - Channel firstChannelForInput = null; - boolean browsableChanged = false; - for (Channel channel : manager.getChannelList()) { - if (channel.getInputId().equals(inputId)) { - if (!channel.isBrowsable()) { - manager.updateBrowsable(channel.getId(), true, true); - browsableChanged = true; + private static void updateChannelsAfterSetup( + Context context, final String inputId, final Runnable postRunnable) { + TvSingletons tvSingletons = TvSingletons.getSingletons(context); + final ChannelDataManager manager = tvSingletons.getChannelDataManager(); + manager.updateChannels( + new Runnable() { + @Override + public void run() { + Channel firstChannelForInput = null; + boolean browsableChanged = false; + for (Channel channel : manager.getChannelList()) { + if (channel.getInputId().equals(inputId)) { + if (!channel.isBrowsable()) { + manager.updateBrowsable(channel.getId(), true, true); + browsableChanged = true; + } + if (firstChannelForInput == null) { + firstChannelForInput = channel; + } + } + } + if (firstChannelForInput != null) { + Utils.setLastWatchedChannel(context, firstChannelForInput); } - if (firstChannelForInput == null) { - firstChannelForInput = channel; + if (browsableChanged) { + manager.notifyChannelBrowsableChanged(); + manager.applyUpdatedValuesToDb(); + } + if (postRunnable != null) { + postRunnable.run(); } } - } - if (firstChannelForInput != null) { - Utils.setLastWatchedChannel(context, firstChannelForInput); - } - if (browsableChanged) { - manager.notifyChannelBrowsableChanged(); - manager.applyUpdatedValuesToDb(); - } - if (postRunnable != null) { - postRunnable.run(); - } - } - }); + }); } - /** - * Marks the channels in newly installed inputs browsable. - */ + /** Marks the channels in newly installed inputs browsable. */ @UiThread public void markNewChannelsBrowsable() { Set<String> newInputsWithChannels = new HashSet<>(); - TvInputManagerHelper tvInputManagerHelper = mTvApplication.getTvInputManagerHelper(); - ChannelDataManager channelDataManager = mTvApplication.getChannelDataManager(); + TvSingletons singletons = TvSingletons.getSingletons(mContext); + TvInputManagerHelper tvInputManagerHelper = singletons.getTvInputManagerHelper(); + ChannelDataManager channelDataManager = singletons.getChannelDataManager(); SoftPreconditions.checkState(channelDataManager.isDbLoadFinished()); for (TvInputInfo input : tvInputManagerHelper.getTvInputInfos(true, true)) { String inputId = input.getId(); @@ -175,9 +168,13 @@ public class SetupUtils { onSetupDone(inputId); newInputsWithChannels.add(inputId); if (DEBUG) { - Log.d(TAG, "New input " + inputId + " has " - + channelDataManager.getChannelCountForInput(inputId) - + " channels"); + Log.d( + TAG, + "New input " + + inputId + + " has " + + channelDataManager.getChannelCountForInput(inputId) + + " channels"); } } } @@ -195,9 +192,7 @@ public class SetupUtils { return mIsFirstTune; } - /** - * Returns true, if the input with {@code inputId} is newly installed. - */ + /** Returns true, if the input with {@code inputId} is newly installed. */ public boolean isNewInput(String inputId) { return !mKnownInputs.contains(inputId); } @@ -209,13 +204,14 @@ public class SetupUtils { public void markAsKnownInput(String inputId) { mKnownInputs.add(inputId); mRecognizedInputs.add(inputId); - mSharedPreferences.edit().putStringSet(PREF_KEY_KNOWN_INPUTS, mKnownInputs) - .putStringSet(PREF_KEY_RECOGNIZED_INPUTS, mRecognizedInputs).apply(); + mSharedPreferences + .edit() + .putStringSet(PREF_KEY_KNOWN_INPUTS, mKnownInputs) + .putStringSet(PREF_KEY_RECOGNIZED_INPUTS, mRecognizedInputs) + .apply(); } - /** - * Returns {@code true}, if {@code inputId}'s setup has been done before. - */ + /** Returns {@code true}, if {@code inputId}'s setup has been done before. */ public boolean isSetupDone(String inputId) { boolean done = mSetUpInputs.contains(inputId); if (DEBUG) { @@ -224,9 +220,7 @@ public class SetupUtils { return done; } - /** - * Returns true, if there is any newly installed input. - */ + /** Returns true, if there is any newly installed input. */ public boolean hasNewInput(TvInputManagerHelper inputManager) { for (TvInputInfo input : inputManager.getTvInputInfos(true, true)) { if (isNewInput(input.getId())) { @@ -236,9 +230,7 @@ public class SetupUtils { return false; } - /** - * Checks whether the given input is already recognized by the user or not. - */ + /** Checks whether the given input is already recognized by the user or not. */ private boolean isRecognizedInput(String inputId) { return mRecognizedInputs.contains(inputId); } @@ -251,13 +243,13 @@ public class SetupUtils { for (TvInputInfo input : inputManager.getTvInputInfos(true, true)) { mRecognizedInputs.add(input.getId()); } - mSharedPreferences.edit().putStringSet(PREF_KEY_RECOGNIZED_INPUTS, mRecognizedInputs) + mSharedPreferences + .edit() + .putStringSet(PREF_KEY_RECOGNIZED_INPUTS, mRecognizedInputs) .apply(); } - /** - * Checks whether there are any unrecognized inputs. - */ + /** Checks whether there are any unrecognized inputs. */ public boolean hasUnrecognizedInput(TvInputManagerHelper inputManager) { for (TvInputInfo input : inputManager.getTvInputInfos(true, true)) { if (!isRecognizedInput(input.getId())) { @@ -276,8 +268,8 @@ public class SetupUtils { // Find all already-verified packages. Set<String> setUpPackages = new HashSet<>(); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); - for (String input : sp.getStringSet(PREF_KEY_SET_UP_INPUTS, - Collections.<String>emptySet())) { + for (String input : + sp.getStringSet(PREF_KEY_SET_UP_INPUTS, Collections.<String>emptySet())) { if (!TextUtils.isEmpty(input)) { ComponentName componentName = ComponentName.unflattenFromString(input); if (componentName != null) { @@ -299,23 +291,28 @@ public class SetupUtils { */ public static void grantEpgPermission(Context context, String packageName) { if (DEBUG) { - Log.d(TAG, "grantEpgPermission(context=" + context + ", packageName=" + packageName - + ")"); + Log.d( + TAG, + "grantEpgPermission(context=" + context + ", packageName=" + packageName + ")"); } try { - int modeFlags = Intent.FLAG_GRANT_WRITE_URI_PERMISSION - | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION; + int modeFlags = + Intent.FLAG_GRANT_WRITE_URI_PERMISSION + | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION; context.grantUriPermission(packageName, TvContract.Channels.CONTENT_URI, modeFlags); context.grantUriPermission(packageName, TvContract.Programs.CONTENT_URI, modeFlags); } catch (SecurityException e) { - Log.e(TAG, "Either TvProvider does not allow granting of Uri permissions or the app" - + " does not have permission.", e); + Log.e( + TAG, + "Either TvProvider does not allow granting of Uri permissions or the app" + + " does not have permission.", + e); } } /** - * Called when Live channels app is launched. Once it is called, {@link - * #isFirstTune} will return false. + * Called when Live channels app is launched. Once it is called, {@link #isFirstTune} will + * return false. */ public void onTuned() { if (!mIsFirstTune) { @@ -325,9 +322,7 @@ public class SetupUtils { mSharedPreferences.edit().putBoolean(PREF_KEY_IS_FIRST_TUNE, false).apply(); } - /** - * Called when input list is changed. It mainly handles input removals. - */ + /** Called when input list is changed. It mainly handles input removals. */ public void onInputListUpdated(TvInputManager manager) { // mRecognizedInputs > mKnownInputs > mSetUpInputs. Set<String> removedInputList = new HashSet<>(mRecognizedInputs); @@ -345,9 +340,10 @@ public class SetupUtils { try { // Just after booting, input list from TvInputManager are not reliable. // So we need to double-check package existence. b/29034900 - mTvApplication.getPackageManager().getPackageInfo( - ComponentName.unflattenFromString(input) - .getPackageName(), PackageManager.GET_ACTIVITIES); + mContext.getPackageManager() + .getPackageInfo( + ComponentName.unflattenFromString(input).getPackageName(), + PackageManager.GET_ACTIVITIES); Log.i(TAG, "TV input (" + input + ") is removed but package is not deleted"); } catch (NameNotFoundException e) { Log.i(TAG, "TV input (" + input + ") and its package are removed"); @@ -358,9 +354,12 @@ public class SetupUtils { } } if (inputPackageDeleted) { - mSharedPreferences.edit().putStringSet(PREF_KEY_SET_UP_INPUTS, mSetUpInputs) + mSharedPreferences + .edit() + .putStringSet(PREF_KEY_SET_UP_INPUTS, mSetUpInputs) .putStringSet(PREF_KEY_KNOWN_INPUTS, mKnownInputs) - .putStringSet(PREF_KEY_RECOGNIZED_INPUTS, mRecognizedInputs).apply(); + .putStringSet(PREF_KEY_RECOGNIZED_INPUTS, mRecognizedInputs) + .apply(); } } } @@ -375,7 +374,9 @@ public class SetupUtils { if (!mRecognizedInputs.contains(inputId)) { Log.i(TAG, "An unrecognized input's setup has been done. inputId=" + inputId); mRecognizedInputs.add(inputId); - mSharedPreferences.edit().putStringSet(PREF_KEY_RECOGNIZED_INPUTS, mRecognizedInputs) + mSharedPreferences + .edit() + .putStringSet(PREF_KEY_RECOGNIZED_INPUTS, mRecognizedInputs) .apply(); } if (!mKnownInputs.contains(inputId)) { @@ -388,4 +389,4 @@ public class SetupUtils { mSharedPreferences.edit().putStringSet(PREF_KEY_SET_UP_INPUTS, mSetUpInputs).apply(); } } -}
\ No newline at end of file +} |