From 1d5fc6e1308bfcfa6f35e38ff17f62cb3b56a513 Mon Sep 17 00:00:00 2001 From: Shubang Date: Tue, 20 Mar 2018 18:36:17 -0700 Subject: Remove preview channels and preview programs when disabled Bug: 69059948 Test: make TvProvider -j30; adb push to priv-app; adb install; and tested with local device. Change-Id: Ie16835582f2e1d57230b7b3af77a850dd22e3008 --- .../providers/tv/PackageChangedReceiver.java | 91 ++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/com/android/providers/tv/PackageChangedReceiver.java (limited to 'src/com/android') diff --git a/src/com/android/providers/tv/PackageChangedReceiver.java b/src/com/android/providers/tv/PackageChangedReceiver.java new file mode 100644 index 0000000..c5348ad --- /dev/null +++ b/src/com/android/providers/tv/PackageChangedReceiver.java @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2018 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.providers.tv; + +import android.content.BroadcastReceiver; +import android.content.ContentProviderOperation; +import android.content.ContentProviderResult; +import android.content.ContentResolver; +import android.content.Context; +import android.content.Intent; +import android.content.OperationApplicationException; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.media.tv.TvContract; +import android.os.RemoteException; +import android.util.Log; + +import java.util.ArrayList; +import java.util.Arrays; + +/** + * This will be launched when PACKAGE_CHANGED intent is broadcast. + */ +public class PackageChangedReceiver extends BroadcastReceiver { + private static final boolean DEBUG = true; + private static final String TAG = "PackageChangedReceiver"; + + @Override + public void onReceive(Context context, Intent intent) { + if (Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction()) && intent.getData() != null) { + String packageName = intent.getData().getSchemeSpecificPart(); + PackageManager pm = context.getPackageManager(); + try { + ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0); + if (appInfo.enabled) { + return; + } + } catch (PackageManager.NameNotFoundException e) { + Log.e(TAG, "Error: package " + packageName + " not found", e); + return; + } + ArrayList operations = new ArrayList<>(); + int uid = intent.getIntExtra(Intent.EXTRA_UID, 0); + + String ProgramsSelection = TvContract.BaseTvColumns.COLUMN_PACKAGE_NAME + "=?"; + String[] ProgramsSelectionArgs = {packageName}; + operations.add(ContentProviderOperation + .newDelete(TvContract.WatchNextPrograms.CONTENT_URI) + .withSelection(ProgramsSelection, ProgramsSelectionArgs).build()); + operations.add(ContentProviderOperation + .newDelete(TvContract.PreviewPrograms.CONTENT_URI) + .withSelection(ProgramsSelection, ProgramsSelectionArgs).build()); + + + String ChannelsSelection = TvContract.BaseTvColumns.COLUMN_PACKAGE_NAME + "=? AND " + + TvContract.Channels.COLUMN_TYPE + "=?"; + String[] ChannelsSelectionArgs = {packageName, TvContract.Channels.TYPE_PREVIEW}; + operations.add(ContentProviderOperation + .newDelete(TvContract.Channels.CONTENT_URI) + .withSelection(ChannelsSelection, ChannelsSelectionArgs).build()); + + ContentProviderResult[] results = null; + try { + ContentResolver cr = context.getContentResolver(); + results = cr.applyBatch(TvContract.AUTHORITY, operations); + } catch (RemoteException | OperationApplicationException e) { + Log.e(TAG, "error in applyBatch", e); + } + + if (DEBUG) { + Log.d(TAG, "onPackageFullyChanged(packageName=" + packageName + ", uid=" + uid + + ")"); + Log.d(TAG, "results=" + Arrays.toString(results)); + } + } + } +} -- cgit v1.2.3