Skip to content

Commit

Permalink
优化: app 获取与选取
Browse files Browse the repository at this point in the history
  • Loading branch information
HChenX committed Jun 11, 2024
1 parent 6eb3a0f commit e6c1562
Showing 1 changed file with 53 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -123,10 +124,10 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
switch (modeSelection) {
case CALLBACK_MODE -> {
mAppSelectCallback.sendMsgToActivity(BitmapUtils.Bitmap2Bytes(appData.icon),
appData.label,
appData.packageName,
appData.versionName + "(" + appData.versionCode + ")",
appData.activityName);
appData.label,
appData.packageName,
appData.versionName + "(" + appData.versionCode + ")",
appData.activityName);
requireActivity().finish();
}
case LAUNCHER_MODE, APP_OPEN_MODE -> {
Expand All @@ -143,11 +144,11 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
}
case INPUT_MODE -> {
showEditDialog(appData.label, new EditDialogCallback() {
@Override
public void onInputReceived(String userInput) {
iEditCallback.editCallback(appData.label, appData.packageName, userInput);
@Override
public void onInputReceived(String userInput) {
iEditCallback.editCallback(appData.label, appData.packageName, userInput);
}
}
}
);
}
}
Expand All @@ -161,17 +162,17 @@ private void showEditDialog(String defaultText, EditDialogCallback callback) {
input.setText(defaultText);

new AlertDialog.Builder(requireActivity())
.setTitle(R.string.edit)
.setView(view)
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
String userInput = input.getText().toString();
callback.onInputReceived(userInput);
dialog.dismiss();
})
.setNegativeButton(android.R.string.cancel, (dialog, which) -> {
dialog.dismiss();
})
.show();
.setTitle(R.string.edit)
.setView(view)
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
String userInput = input.getText().toString();
callback.onInputReceived(userInput);
dialog.dismiss();
})
.setNegativeButton(android.R.string.cancel, (dialog, which) -> {
dialog.dismiss();
})
.show();
}

private void initData() {
Expand All @@ -183,7 +184,7 @@ public void run() {
public void run() {
appDataList = getAppInfo();
mAppListAdapter = new AppDataAdapter(requireActivity(),
R.layout.item_app_list, appDataList, key, modeSelection);
R.layout.item_app_list, appDataList, key, modeSelection);
mAppListRv.setAdapter(mAppListAdapter);
mAmProgress.setVisibility(View.GONE);
mAppListRv.setVisibility(View.VISIBLE);
Expand All @@ -196,26 +197,40 @@ public void run() {
public List<AppData> getAppInfo() {
return switch (modeSelection) {
case LAUNCHER_MODE, CALLBACK_MODE, INPUT_MODE ->
PackagesUtils.getPackagesByCode(new PackagesUtils.IPackageCode() {
@Override
public List<Parcelable> getPackageCodeList(PackageManager pm) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfoList = new ArrayList<>();
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);
hashMap.clear();
for (ResolveInfo resolveInfo : resolveInfos) {
Integer added = hashMap.get(resolveInfo.activityInfo.applicationInfo.packageName);
if (added == null || added != 1) {
hashMap.put(resolveInfo.activityInfo.applicationInfo.packageName, 1);
} else {
continue;
PackagesUtils.getPackagesByCode(new PackagesUtils.IPackageCode() {
@Override
public List<Parcelable> getPackageCodeList(PackageManager pm) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfoList = new ArrayList<>();
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);
List<ResolveInfo> resolveInfosHaveNoLauncher = pm.queryIntentActivities(new Intent(Intent.ACTION_MAIN), PackageManager.GET_ACTIVITIES);
if (resolveInfosHaveNoLauncher.size() > resolveInfos.size()) {
Iterator<ResolveInfo> iterator = resolveInfosHaveNoLauncher.iterator();
while (iterator.hasNext()) {
ResolveInfo info = iterator.next();
if (resolveInfos.contains(info)) {
continue;
} else {
if (PackagesUtils.isSystem(info.activityInfo.applicationInfo)) {
iterator.remove();
}
}
}
}
resolveInfoList.add(resolveInfo);
hashMap.clear();
for (ResolveInfo resolveInfo : resolveInfosHaveNoLauncher) {
Integer added = hashMap.get(resolveInfo.activityInfo.applicationInfo.packageName);
if (added == null || added != 1) {
hashMap.put(resolveInfo.activityInfo.applicationInfo.packageName, 1);
} else {
continue;
}
resolveInfoList.add(resolveInfo);
}
return new ArrayList<>(resolveInfoList);
}
return new ArrayList<>(resolveInfoList);
}
});
});
case APP_OPEN_MODE -> PackagesUtils.getOpenWithApps();
default -> new ArrayList<>();
};
Expand Down

0 comments on commit e6c1562

Please sign in to comment.