增加app列表页面,app数据存入数据库,优化icon cache
This commit is contained in:
1 parent
56341f6888
commit
5cc7caf34f
33 files changed
+1930
-115
No files matched your search
@@ -0,0 +1,73 @@
|
||||
package com.ttstd.dialer.activity.app;
|
||||
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.loader.app.LoaderManager;
|
||||
import androidx.recyclerview.widget.GridLayoutManager;
|
||||
|
||||
import com.tencent.mmkv.MMKV;
|
||||
import com.ttstd.dialer.R;
|
||||
import com.ttstd.dialer.adapter.MoreAppAdapter;
|
||||
import com.ttstd.dialer.base.mvvm.BaseMvvmActivity;
|
||||
import com.ttstd.dialer.db.app.DesktopSortApp;
|
||||
import com.ttstd.dialer.config.CommonConfig;
|
||||
import com.ttstd.dialer.databinding.ActivityAppListBinding;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class AppListActivity extends BaseMvvmActivity<AppListViewModel, ActivityAppListBinding> {
|
||||
private static final String TAG = "AppListActivity";
|
||||
|
||||
private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
|
||||
|
||||
private MoreAppAdapter mMoreAppAdapter;
|
||||
|
||||
|
||||
@Override
|
||||
public boolean setNightMode() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setfitWindow() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getLayoutId() {
|
||||
return R.layout.activity_app_list;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initDataBinding() {
|
||||
mViewModel.setContext(this);
|
||||
mViewModel.setVDBinding(mViewDataBinding);
|
||||
mViewModel.setLifecycle(getLifecycleSubject());
|
||||
mViewDataBinding.setClick(new BtnClick());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initView() {
|
||||
mMoreAppAdapter = new MoreAppAdapter(LoaderManager.getInstance(this));
|
||||
mViewDataBinding.recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
|
||||
mViewDataBinding.recyclerView.setAdapter(mMoreAppAdapter);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initData() {
|
||||
mViewModel.mDesktopSortAppData.observe(this, new Observer<List<DesktopSortApp>>() {
|
||||
@Override
|
||||
public void onChanged(List<DesktopSortApp> desktopSortApps) {
|
||||
mMoreAppAdapter.setDesktopSortApps(desktopSortApps);
|
||||
}
|
||||
});
|
||||
|
||||
// mViewModel.getLauncherAppList();
|
||||
mViewModel.getDbAppList();
|
||||
}
|
||||
|
||||
|
||||
public class BtnClick {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package com.ttstd.dialer.activity.app;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import com.trello.rxlifecycle4.RxLifecycle;
|
||||
import com.trello.rxlifecycle4.android.ActivityEvent;
|
||||
import com.ttstd.dialer.base.mvvm.BaseViewModel;
|
||||
import com.ttstd.dialer.db.app.AppRepository;
|
||||
import com.ttstd.dialer.db.app.DesktopSortApp;
|
||||
import com.ttstd.dialer.databinding.ActivityAppListBinding;
|
||||
import com.ttstd.dialer.utils.ApkUtils;
|
||||
|
||||
import java.text.Collator;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.rxjava3.annotations.NonNull;
|
||||
import io.reactivex.rxjava3.core.Observable;
|
||||
import io.reactivex.rxjava3.core.Observer;
|
||||
import io.reactivex.rxjava3.disposables.Disposable;
|
||||
import io.reactivex.rxjava3.functions.Function;
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||
|
||||
public class AppListViewModel extends BaseViewModel<ActivityAppListBinding, ActivityEvent> {
|
||||
private static final String TAG = "AppListViewModel";
|
||||
|
||||
private AppRepository mAppRepository;
|
||||
|
||||
@Override
|
||||
public void setContext(Context context) {
|
||||
super.setContext(context);
|
||||
mAppRepository = new AppRepository(context);
|
||||
}
|
||||
|
||||
public MutableLiveData<List<DesktopSortApp>> mDesktopSortAppData = new MutableLiveData<>();
|
||||
|
||||
public void getDbAppList(){
|
||||
Observable.fromCallable(new Callable<List<DesktopSortApp>>() {
|
||||
@Override
|
||||
public List<DesktopSortApp> call() throws Exception {
|
||||
return mAppRepository.getAllContacts();
|
||||
}
|
||||
}) .compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Observer<List<DesktopSortApp>>() {
|
||||
@Override
|
||||
public void onSubscribe(@NonNull Disposable d) {
|
||||
Log.e("getDbAppList", "onSubscribe: " );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(@NonNull List<DesktopSortApp> desktopSortApps) {
|
||||
Log.e("getDbAppList", "onNext: "+desktopSortApps );
|
||||
mDesktopSortAppData.setValue(desktopSortApps);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(@NonNull Throwable e) {
|
||||
Log.e("getDbAppList", "onError: " +e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
Log.e("getDbAppList", "onComplete: " );
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void getLauncherAppList() {
|
||||
Observable.fromCallable(new Callable<List<ResolveInfo>>() {
|
||||
@Override
|
||||
public List<ResolveInfo> call() throws Exception {
|
||||
return ApkUtils.getAllLauncherResolveInfo(getSafeContext());
|
||||
}
|
||||
})
|
||||
.compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.map(new Function<List<ResolveInfo>, List<ComponentName>>() {
|
||||
@Override
|
||||
public List<ComponentName> apply(List<ResolveInfo> resolveInfos) throws Throwable {
|
||||
List<ComponentName> componentNames = resolveInfos.stream().map(new java.util.function.Function<ResolveInfo, ComponentName>() {
|
||||
@Override
|
||||
public ComponentName apply(ResolveInfo resolveInfo) {
|
||||
String packageName = resolveInfo.activityInfo.packageName;
|
||||
String className = resolveInfo.activityInfo.name;
|
||||
ComponentName componentName = new ComponentName(packageName, className);
|
||||
return componentName;
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
return componentNames;
|
||||
}
|
||||
})
|
||||
.compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.map(new Function<List<ComponentName>, List<DesktopSortApp>>() {
|
||||
@Override
|
||||
public List<DesktopSortApp> apply(List<ComponentName> componentNames) throws Throwable {
|
||||
List<DesktopSortApp> desktopSortApps = componentNames.stream().map(new java.util.function.Function<ComponentName, DesktopSortApp>() {
|
||||
@Override
|
||||
public DesktopSortApp apply(ComponentName componentName) {
|
||||
DesktopSortApp desktopSortApp = new DesktopSortApp(getSafeContext(), componentName);
|
||||
return desktopSortApp;
|
||||
}
|
||||
}).sorted(new Comparator<DesktopSortApp>() {
|
||||
@Override
|
||||
public int compare(DesktopSortApp o1, DesktopSortApp o2) {
|
||||
return Collator.getInstance(Locale.CHINESE).compare(o1.getLabel(), o2.getLabel());
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
return desktopSortApps;
|
||||
}
|
||||
})
|
||||
.compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Observer<List<DesktopSortApp>>() {
|
||||
@Override
|
||||
public void onSubscribe(@NonNull Disposable d) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(@NonNull List<DesktopSortApp> desktopSortApps) {
|
||||
Log.e(TAG, "getLauncherAppList" + "onNext: " + desktopSortApps);
|
||||
mDesktopSortAppData.setValue(desktopSortApps);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(@NonNull Throwable e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -102,7 +102,7 @@ public class MainActivity extends BaseMvvmActivity<MainViewModel, ActivityMainBi
|
||||
|
||||
@Override
|
||||
protected void initData() {
|
||||
|
||||
mViewModel.updateAppList();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,9 +1,112 @@
|
||||
package com.ttstd.dialer.activity.main;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.util.Log;
|
||||
|
||||
import com.trello.rxlifecycle4.RxLifecycle;
|
||||
import com.trello.rxlifecycle4.android.ActivityEvent;
|
||||
import com.ttstd.dialer.base.mvvm.BaseViewModel;
|
||||
import com.ttstd.dialer.databinding.ActivityMainBinding;
|
||||
import com.ttstd.dialer.db.app.AppRepository;
|
||||
import com.ttstd.dialer.db.app.DesktopSortApp;
|
||||
import com.ttstd.dialer.utils.ApkUtils;
|
||||
|
||||
import java.text.Collator;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.rxjava3.annotations.NonNull;
|
||||
import io.reactivex.rxjava3.core.Observable;
|
||||
import io.reactivex.rxjava3.core.Observer;
|
||||
import io.reactivex.rxjava3.disposables.Disposable;
|
||||
import io.reactivex.rxjava3.functions.Function;
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||
|
||||
public class MainViewModel extends BaseViewModel<ActivityMainBinding, ActivityEvent> {
|
||||
private static final String TAG = "MainViewModel";
|
||||
|
||||
|
||||
private AppRepository mAppRepository;
|
||||
|
||||
@Override
|
||||
public void setContext(Context context) {
|
||||
super.setContext(context);
|
||||
mAppRepository = new AppRepository(context);
|
||||
}
|
||||
|
||||
public void updateAppList() {
|
||||
Observable.fromCallable(new Callable<List<ResolveInfo>>() {
|
||||
@Override
|
||||
public List<ResolveInfo> call() throws Exception {
|
||||
return ApkUtils.getAllLauncherResolveInfo(getSafeContext());
|
||||
}
|
||||
})
|
||||
.compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.map(new Function<List<ResolveInfo>, List<ComponentName>>() {
|
||||
@Override
|
||||
public List<ComponentName> apply(List<ResolveInfo> resolveInfos) throws Throwable {
|
||||
List<ComponentName> componentNames = resolveInfos.stream().map(new java.util.function.Function<ResolveInfo, ComponentName>() {
|
||||
@Override
|
||||
public ComponentName apply(ResolveInfo resolveInfo) {
|
||||
String packageName = resolveInfo.activityInfo.packageName;
|
||||
String className = resolveInfo.activityInfo.name;
|
||||
ComponentName componentName = new ComponentName(packageName, className);
|
||||
return componentName;
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
return componentNames;
|
||||
}
|
||||
})
|
||||
.compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.map(new Function<List<ComponentName>, List<DesktopSortApp>>() {
|
||||
@Override
|
||||
public List<DesktopSortApp> apply(List<ComponentName> componentNames) throws Throwable {
|
||||
List<DesktopSortApp> desktopSortApps = componentNames.stream().map(new java.util.function.Function<ComponentName, DesktopSortApp>() {
|
||||
@Override
|
||||
public DesktopSortApp apply(ComponentName componentName) {
|
||||
DesktopSortApp desktopSortApp = new DesktopSortApp(getSafeContext(), componentName);
|
||||
return desktopSortApp;
|
||||
}
|
||||
}).sorted(new Comparator<DesktopSortApp>() {
|
||||
@Override
|
||||
public int compare(DesktopSortApp o1, DesktopSortApp o2) {
|
||||
return Collator.getInstance(Locale.CHINESE).compare(o1.getLabel(), o2.getLabel());
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
return desktopSortApps;
|
||||
}
|
||||
})
|
||||
.compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Observer<List<DesktopSortApp>>() {
|
||||
@Override
|
||||
public void onSubscribe(@NonNull Disposable d) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(@NonNull List<DesktopSortApp> desktopSortApps) {
|
||||
Log.e(TAG, "getLauncherAppList" + "onNext: " + desktopSortApps);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(@NonNull Throwable e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user