1 /*
2  * Copyright (C) 2018 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 
17 package com.android.providers.tv;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.ContentProviderOperation;
21 import android.content.ContentProviderResult;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.OperationApplicationException;
26 import android.content.pm.ApplicationInfo;
27 import android.content.pm.PackageManager;
28 import android.media.tv.TvContract;
29 import android.os.RemoteException;
30 import android.util.Log;
31 
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 
35 /**
36  * This will be launched when PACKAGE_CHANGED intent is broadcast.
37  */
38 public class PackageChangedReceiver extends BroadcastReceiver {
39     private static final boolean DEBUG = true;
40     private static final String TAG = "PackageChangedReceiver";
41 
42     @Override
onReceive(Context context, Intent intent)43     public void onReceive(Context context, Intent intent) {
44         if (Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction()) && intent.getData() != null) {
45             String packageName = intent.getData().getSchemeSpecificPart();
46             PackageManager pm = context.getPackageManager();
47             try {
48                 ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0);
49                 if (appInfo.enabled) {
50                     return;
51                 }
52             } catch (PackageManager.NameNotFoundException e) {
53                 Log.e(TAG, "Error: package " + packageName + " not found", e);
54                 return;
55             }
56             ArrayList<ContentProviderOperation> operations = new ArrayList<>();
57             int uid = intent.getIntExtra(Intent.EXTRA_UID, 0);
58 
59             String ProgramsSelection = TvContract.BaseTvColumns.COLUMN_PACKAGE_NAME + "=?";
60             String[] ProgramsSelectionArgs = {packageName};
61             operations.add(ContentProviderOperation
62                     .newDelete(TvContract.WatchNextPrograms.CONTENT_URI)
63                     .withSelection(ProgramsSelection, ProgramsSelectionArgs).build());
64             operations.add(ContentProviderOperation
65                     .newDelete(TvContract.PreviewPrograms.CONTENT_URI)
66                     .withSelection(ProgramsSelection, ProgramsSelectionArgs).build());
67 
68 
69             String ChannelsSelection = TvContract.BaseTvColumns.COLUMN_PACKAGE_NAME + "=? AND "
70                     + TvContract.Channels.COLUMN_TYPE + "=?";
71             String[] ChannelsSelectionArgs = {packageName, TvContract.Channels.TYPE_PREVIEW};
72             operations.add(ContentProviderOperation
73                     .newDelete(TvContract.Channels.CONTENT_URI)
74                     .withSelection(ChannelsSelection, ChannelsSelectionArgs).build());
75 
76             ContentProviderResult[] results = null;
77             try {
78                 ContentResolver cr = context.getContentResolver();
79                 results = cr.applyBatch(TvContract.AUTHORITY, operations);
80             } catch (RemoteException | OperationApplicationException e) {
81                 Log.e(TAG, "error in applyBatch", e);
82             }
83 
84             if (DEBUG) {
85                 Log.d(TAG, "onPackageFullyChanged(packageName=" + packageName + ", uid=" + uid
86                         + ")");
87                 Log.d(TAG, "results=" + Arrays.toString(results));
88             }
89         }
90     }
91 }
92