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