android launcher3 安裝解除安裝
阿新 • • 發佈:2019-02-18
launcher3安裝解除安裝流程
當手機安裝解除安裝應用時,桌面圖示會跟隨發生變化(單層)、主選單圖示發生變化(雙層)。而,能做到這一點,launcher必須知道什麼時候安裝解除安裝,什麼時候需要新增或者刪除圖示。本篇文章的觸發點就在於,廣播接受者!!!1、廣播註冊、接受
對於安裝解除安裝更新的廣播註冊,是在程式碼裡動態進行的。
LauncherAppState的構造方法中:
// Register intent receivers IntentFilter filter = new IntentFilter( Intent.ACTION_PACKAGE_ADDED );//安裝應用 filter.addAction( Intent.ACTION_PACKAGE_REMOVED );//解除安裝應用 filter.addAction( Intent.ACTION_PACKAGE_CHANGED );//更新應用 filter.addDataScheme( "package" ); sContext.registerReceiver( mModel , filter );
註冊廣播後,LauncherModel就可以接受應用的安裝解除安裝更新廣播,進行相應的處理:
接到廣播後,通過action值設定不同的op,對應不同的操作。final String action = intent.getAction(); if( Intent.ACTION_PACKAGE_CHANGED.equals( action ) || Intent.ACTION_PACKAGE_REMOVED.equals( action ) || Intent.ACTION_PACKAGE_ADDED.equals( action ) ) { final String packageName = intent.getData().getSchemeSpecificPart(); final boolean replacing = intent.getBooleanExtra( Intent.EXTRA_REPLACING , false ); int op = PackageUpdatedTask.OP_NONE; if( packageName == null || packageName.length() == 0 ) { // they sent us a bad intent return; } if( Intent.ACTION_PACKAGE_CHANGED.equals( action ) ) { op = PackageUpdatedTask.OP_UPDATE; } else if( Intent.ACTION_PACKAGE_REMOVED.equals( action ) ) { if( !replacing ) { op = PackageUpdatedTask.OP_REMOVE; } // else, we are replacing the package, so a PACKAGE_ADDED will be sent // later, we will update the package at this time } else if( Intent.ACTION_PACKAGE_ADDED.equals( action ) ) { if( !replacing ) { op = PackageUpdatedTask.OP_ADD; } else { op = PackageUpdatedTask.OP_UPDATE; } } enqueuePackageUpdatedApp( op , new String[]{ packageName } ); }
/**
* 通過對應的op值和packageName去新增,解除安裝,更新應用
* @param op
* @param packageName
*/
public void enqueuePackageUpdatedApp(
int op ,
String[] packages )
{
if( op != PackageUpdatedTask.OP_NONE )
{
enqueuePackageUpdated( new PackageUpdatedTask( op , packages ) );
}
}
2、安裝、解除安裝、更新
public static final int OP_ADD = 1;
public static final int OP_UPDATE = 2;
public static final int OP_REMOVE = 3; // uninstlled
PackageUpdatedTask implements Runnable,run方法中具體執行了安裝、解除安裝、更新、等廣播的操作。
通過switch case 判斷當前操作(安裝、更新、解除安裝),一下以安裝為例:
1、case OP_ADD:迴圈安裝應用的包名陣列,並進行mBgAllAppsList.addPackage( context , packages[i] );操作。進入AllAppsList類中
public void addPackage(
Context context ,
String packageName )
{
final List<ResolveInfo> matches = findActivitiesForPackage( context , packageName );
if( matches.size() > 0 )
{
for( ResolveInfo info : matches )
{
add( new AppInfo( context.getPackageManager() , info , mIconCache , null ) );
}
}
}
首先通過包名獲得應用的ResolveInfo,然後把應用add進去。add方法中對info進行去重新增,程式碼就不貼出來了。
AllAppsList類的主要幾個成員變數:
public static final int DEFAULT_APPLICATIONS_NUMBER = 42;
/** The list off all apps. */
public ArrayList<AppInfo> data = new ArrayList<AppInfo>( DEFAULT_APPLICATIONS_NUMBER );//所有應用的info列表
/** The list of apps that have been added since the last notify() call. */
public ArrayList<AppInfo> added = new ArrayList<AppInfo>( DEFAULT_APPLICATIONS_NUMBER );//安裝應用info列表
/** The list of apps that have been removed since the last notify() call. */
public ArrayList<AppInfo> removed = new ArrayList<AppInfo>();//解除安裝應用的info列表
/** The list of apps that have been modified since the last notify() call. */
public ArrayList<AppInfo> modified = new ArrayList<AppInfo>();//更新應用的info列表
至此,安裝的應用就被新增到了ArrayList<AppInfo> added中。其他操作同理,之後就是對列表資料的處理,以及桌面圖示的變化和資料庫的操作。
2、把安裝應用資訊儲存在臨時列表,清空AllAppsList類中的added列表資料
if( mBgAllAppsList.added.size() > 0 )
{
added = new ArrayList<AppInfo>( mBgAllAppsList.added );
mBgAllAppsList.added.clear();
}
3、新增圖示
final ArrayList<ItemInfo> addedInfos = new ArrayList<ItemInfo>( added );
addAndBindAddedItems( context , addedInfos , null , cb , added , true , !mIsLoaderTaskRunning );
addAndBindAddedItems中的操作在http://blog.csdn.net/a396604593/article/details/52794920中有所介紹。3、後記
這篇流程和邏輯比較簡單,安裝、更新、解除安裝應用。從幾個點出發:
1、怎麼獲得應用安裝、更新、解除安裝的訊息。(註冊對應廣播,通過不同的action區分不同操作)
2、知道對應的操作和,如何處理資料(launcherModel中的內部類PackageUpdatedTask裡面負責資料處理和呼叫,應用資訊資料儲存在AllAppsList類中)
3、介面上如何對應資料變化(新增圖示、更新圖示、刪除圖示回撥launcher中對應的方法)
具體的邏輯還需要對照程式碼來看一下細節問題,比如應用是否安裝、計算安裝應用的位置、更新或者解除安裝桌面圖示、等等。