优化方法名,去掉无用代码

This commit is contained in:
2025-10-31 10:09:05 +08:00
parent 5cc7caf34f
commit 59b6c9db76
7 changed files with 41 additions and 123 deletions

View File

@@ -23,7 +23,7 @@ android {
ndk {
//根据需要 自行选择添加的对应cpu类型的.so库。
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
// 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
// 还可以添加 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64', 'mips', 'mips64'
}
lintOptions {

View File

@@ -10,9 +10,9 @@ 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.databinding.ActivityAppListBinding;
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;
@@ -43,35 +43,35 @@ public class AppListViewModel extends BaseViewModel<ActivityAppListBinding, Acti
public MutableLiveData<List<DesktopSortApp>> mDesktopSortAppData = new MutableLiveData<>();
public void getDbAppList(){
public void getDbAppList() {
Observable.fromCallable(new Callable<List<DesktopSortApp>>() {
@Override
public List<DesktopSortApp> call() throws Exception {
return mAppRepository.getAllContacts();
return mAppRepository.getAllApp();
}
}) .compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
}).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: " );
Log.e("getDbAppList", "onSubscribe: ");
}
@Override
public void onNext(@NonNull List<DesktopSortApp> desktopSortApps) {
Log.e("getDbAppList", "onNext: "+desktopSortApps );
Log.e("getDbAppList", "onNext: " + desktopSortApps);
mDesktopSortAppData.setValue(desktopSortApps);
}
@Override
public void onError(@NonNull Throwable e) {
Log.e("getDbAppList", "onError: " +e.getMessage());
Log.e("getDbAppList", "onError: " + e.getMessage());
}
@Override
public void onComplete() {
Log.e("getDbAppList", "onComplete: " );
Log.e("getDbAppList", "onComplete: ");
}
});

View File

@@ -102,7 +102,7 @@ public class MainActivity extends BaseMvvmActivity<MainViewModel, ActivityMainBi
@Override
protected void initData() {
mViewModel.updateAppList();
}
@Override

View File

@@ -1,37 +1,15 @@
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
@@ -40,73 +18,4 @@ public class MainViewModel extends BaseViewModel<ActivityMainBinding, ActivityEv
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() {
}
});
}
}

View File

@@ -32,7 +32,7 @@ public interface AppDao {
long insert(DesktopSortApp desktopSortApp);
@Insert
long[] insert(List<DesktopSortApp> contacts);
long[] insert(List<DesktopSortApp> desktopSortApps);
@Update
Integer update(DesktopSortApp desktopSortApp);

View File

@@ -33,37 +33,37 @@ public class AppRepository {
}
// 获取所有APP
public List<DesktopSortApp> getAllContacts() {
public List<DesktopSortApp> getAllApp() {
return mAppDao.getAllApp();
}
// 根据ID获取APP
public DesktopSortApp getContactById(int id) {
public DesktopSortApp getAppById(int id) {
return mAppDao.getAppById(id);
}
// 搜索APP
public List<DesktopSortApp> searchContacts(String query) {
public List<DesktopSortApp> searchApp(String query) {
return mAppDao.searchApp("%" + query + "%");
}
// 添加APP
public long insert(DesktopSortApp contact) {
return mAppDao.insert(contact);
public long insert(DesktopSortApp desktopSortApp) {
return mAppDao.insert(desktopSortApp);
}
public long[] insert(List<DesktopSortApp> contacts) {
return mAppDao.insert(contacts);
public long[] insert(List<DesktopSortApp> desktopSortApps) {
return mAppDao.insert(desktopSortApps);
}
// 更新APP
public int update(DesktopSortApp contact) {
return mAppDao.update(contact);
public int update(DesktopSortApp desktopSortApp) {
return mAppDao.update(desktopSortApp);
}
// 删除APP
public int delete(DesktopSortApp contact) {
return mAppDao.delete(contact);
public int delete(DesktopSortApp desktopSortApp) {
return mAppDao.delete(desktopSortApp);
}
// 根据ID删除APP

View File

@@ -23,7 +23,9 @@ import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
@@ -32,19 +34,31 @@ import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class AppManager {
private static final String TAG = "AppManager";
private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
@SuppressLint("StaticFieldLeak")
private static AppManager INSTANCE;
private Context mContext;
private AppRepository mAppRepository;
private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
private Set<ProgressCallback> mProgressCallbacks = new CopyOnWriteArraySet<>();
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
private static final int CORE_POOL_SIZE = Math.max(2, Math.min(CPU_COUNT - 1, 4));
private static final ExecutorService ASYNC_EXECUTOR = Executors.newFixedThreadPool(CORE_POOL_SIZE);
public interface ProgressCallback {
void onProgress(String step, int current, int total);
void onCompleted(boolean success, String message);
}
public void addProgressCallback(ProgressCallback progressCallback) {
mProgressCallbacks.add(progressCallback);
}
public static final List<String> DEFAULT_APP_PACKAGES = new ArrayList<String>() {{
this.add("com.android.settings");
@@ -74,11 +88,6 @@ public class AppManager {
executeAppListProcessing();
}
public interface ProgressCallback {
void onProgress(String step, int current, int total);
void onCompleted(boolean success, String message);
}
// 异步执行方法
public void executeAppListProcessing() {
@@ -107,7 +116,7 @@ public class AppManager {
// 第一步:获取所有桌面应用(增加空值安全处理)
private List<DesktopSortApp> getAllDesktopSortApps() {
try {
List<DesktopSortApp> result = mAppRepository.getAllContacts();
List<DesktopSortApp> result = mAppRepository.getAllApp();
return result != null ? result : Collections.emptyList();
} catch (Exception e) {
Log.e("AppListProcessor", "获取桌面应用列表失败", e);
@@ -117,7 +126,7 @@ public class AppManager {
private Optional<List<DesktopSortApp>> getAllDesktopSortAppsSafe() {
try {
return Optional.ofNullable(mAppRepository.getAllContacts())
return Optional.ofNullable(mAppRepository.getAllApp())
.filter(list -> !list.isEmpty());
} catch (Exception e) {
Log.e("AppListProcessor", "获取应用列表失败", e);
@@ -286,7 +295,7 @@ public class AppManager {
}
public void getAllApp() {
List<DesktopSortApp> desktopSortApps = mAppRepository.getAllContacts();
List<DesktopSortApp> desktopSortApps = mAppRepository.getAllApp();
List<Integer> ids = desktopSortApps.stream().filter(new Predicate<DesktopSortApp>() {
@Override
public boolean test(DesktopSortApp desktopSortApp) {