• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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.gallery3d.app;
18 
19 import android.app.IntentService;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.SharedPreferences;
24 import android.preference.PreferenceManager;
25 import androidx.core.app.JobIntentService;
26 
27 import com.android.gallery3d.picasasource.PicasaSource;
28 import com.android.gallery3d.util.LightCycleHelper;
29 
30 public class PackagesMonitor extends BroadcastReceiver {
31     public static final String KEY_PACKAGES_VERSION  = "packages-version";
32 
getPackagesVersion(Context context)33     public synchronized static int getPackagesVersion(Context context) {
34         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
35         return prefs.getInt(KEY_PACKAGES_VERSION, 1);
36     }
37 
38     @Override
onReceive(final Context context, final Intent intent)39     public void onReceive(final Context context, final Intent intent) {
40         intent.setClass(context, AsyncService.class);
41         AsyncService.enqueueWork(context, intent);
42     }
43 
44     public static class AsyncService extends JobIntentService {
45         public static final int JOB_ID = 1;
46 
enqueueWork(Context context, Intent work)47         public static void enqueueWork(Context context, Intent work) {
48             enqueueWork(context, AsyncService.class, JOB_ID, work);
49         }
50 
51         @Override
onHandleWork(Intent intent)52         protected void onHandleWork(Intent intent) {
53             onReceiveAsync(this, intent);
54         }
55     }
56 
57     // Runs in a background thread.
onReceiveAsync(Context context, Intent intent)58     private static void onReceiveAsync(Context context, Intent intent) {
59         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
60 
61         int version = prefs.getInt(KEY_PACKAGES_VERSION, 1);
62         prefs.edit().putInt(KEY_PACKAGES_VERSION, version + 1).commit();
63 
64         String action = intent.getAction();
65         String packageName = intent.getData().getSchemeSpecificPart();
66         if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
67             PicasaSource.onPackageAdded(context, packageName);
68         } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
69             PicasaSource.onPackageRemoved(context, packageName);
70         } else if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
71             PicasaSource.onPackageChanged(context, packageName);
72         }
73     }
74 }
75