This commit is contained in:
wangcongtao
2020-01-13 21:40:48 +08:00
16 changed files with 318 additions and 112 deletions

View File

@@ -0,0 +1,43 @@
package com.mogo.module.apps;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
/**
* @author congtaowang
* @since 2020-01-13
* <p>
* 描述
*/
public class AppsListChangedLiveData extends MutableLiveData {
private Observer mObserver;
private AppsListChangedLiveData(){
// private constructor
}
private static final class InstanceHolder{
private static final AppsListChangedLiveData INSTANCE = new AppsListChangedLiveData();
}
public static AppsListChangedLiveData getInstance(){
return InstanceHolder.INSTANCE;
}
private Object readResolve(){
// 阻止反序列化,必须实现 Serializable 接口
return InstanceHolder.INSTANCE;
}
@Override
public void observeForever( @NonNull Observer observer ) {
super.observeForever( observer );
mObserver = observer;
}
public void release(){
removeObserver( mObserver );
}
}

View File

@@ -30,6 +30,11 @@ public class AppsPresenter extends Presenter< AppsView > {
public void onCreate( @NonNull LifecycleOwner owner ) {
super.onCreate( owner );
renderAppsList();
AppsListChangedLiveData.getInstance().observeForever( bool -> {
if ( mView != null ) {
renderAppsList();
}
} );
}
private void renderAppsList() {
@@ -62,6 +67,7 @@ public class AppsPresenter extends Presenter< AppsView > {
@Override
public void onDestroy( @NonNull LifecycleOwner owner ) {
super.onDestroy( owner );
AppsListChangedLiveData.getInstance().release();
mView = null;
}
}

View File

@@ -9,6 +9,7 @@ import android.text.TextUtils;
import com.mogo.module.apps.AppFilter;
import com.mogo.module.apps.AppFilterImpl;
import com.mogo.module.apps.AppsConst;
import com.mogo.module.apps.AppsListChangedLiveData;
import java.util.ArrayList;
import java.util.HashMap;
@@ -112,6 +113,7 @@ public class AppsModel {
}
mPagedApps.get( pageIndex ).add( appInfo );
}
AppsListChangedLiveData.getInstance().postValue( true );
} catch ( PackageManager.NameNotFoundException e ) {
e.printStackTrace();
}
@@ -153,32 +155,29 @@ public class AppsModel {
return;
}
try {
final PackageManager packageManager = mContext.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo( packageName, 0 );
if ( !mAppFilter.filter( packageInfo ) ) {
int pageIndex = getPageIndex( packageName, false );
if ( pageIndex == -1 ) {
return;
}
int totalPages = mPagedApps.size();
List< AppInfo > dynamicAppsList = new ArrayList<>();
for ( int i = pageIndex; i < totalPages; i++ ) {
dynamicAppsList.addAll( mPagedApps.remove( i ) );
}
int counter = 0;
for ( int i = 0; i < dynamicAppsList.size(); i++ ) {
AppInfo appInfo = dynamicAppsList.get( i );
if ( TextUtils.equals( appInfo.getPackageName(), packageName ) ) {
continue;
}
int page = counter++ / AppsConst.TOTAL_SIZE_EACH_PAGE + pageIndex;
if ( !mPagedApps.containsKey( page ) ) {
mPagedApps.put( page, new ArrayList<>() );
}
mPagedApps.get( page ).add( appInfo );
}
int pageIndex = getPageIndex( packageName, false );
if ( pageIndex == -1 ) {
return;
}
} catch ( PackageManager.NameNotFoundException e ) {
int totalPages = mPagedApps.size();
List< AppInfo > dynamicAppsList = new ArrayList<>();
for ( int i = pageIndex; i < totalPages; i++ ) {
dynamicAppsList.addAll( mPagedApps.remove( i ) );
}
int counter = 0;
for ( int i = 0; i < dynamicAppsList.size(); i++ ) {
AppInfo appInfo = dynamicAppsList.get( i );
if ( TextUtils.equals( appInfo.getPackageName(), packageName ) ) {
continue;
}
int page = counter++ / AppsConst.TOTAL_SIZE_EACH_PAGE + pageIndex;
if ( !mPagedApps.containsKey( page ) ) {
mPagedApps.put( page, new ArrayList<>() );
}
mPagedApps.get( page ).add( appInfo );
}
AppsListChangedLiveData.getInstance().postValue( true );
} catch ( Exception e ) {
e.printStackTrace();
}
}