init global config view and app list view
This commit is contained in:
73
configapp/src/main/java/com/jiqiu/configapp/AppInfo.java
Normal file
73
configapp/src/main/java/com/jiqiu/configapp/AppInfo.java
Normal file
@@ -0,0 +1,73 @@
|
||||
package com.jiqiu.configapp;
|
||||
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
/**
|
||||
* 应用程序信息数据模型
|
||||
*/
|
||||
public class AppInfo {
|
||||
private String appName; // 应用名称
|
||||
private String packageName; // 包名
|
||||
private Drawable appIcon; // 应用图标
|
||||
private boolean isSystemApp; // 是否为系统应用
|
||||
private boolean isEnabled; // 是否启用注入
|
||||
|
||||
public AppInfo(String appName, String packageName, Drawable appIcon, boolean isSystemApp) {
|
||||
this.appName = appName;
|
||||
this.packageName = packageName;
|
||||
this.appIcon = appIcon;
|
||||
this.isSystemApp = isSystemApp;
|
||||
this.isEnabled = false; // 默认不启用注入
|
||||
}
|
||||
|
||||
// Getter 和 Setter 方法
|
||||
public String getAppName() {
|
||||
return appName;
|
||||
}
|
||||
|
||||
public void setAppName(String appName) {
|
||||
this.appName = appName;
|
||||
}
|
||||
|
||||
public String getPackageName() {
|
||||
return packageName;
|
||||
}
|
||||
|
||||
public void setPackageName(String packageName) {
|
||||
this.packageName = packageName;
|
||||
}
|
||||
|
||||
public Drawable getAppIcon() {
|
||||
return appIcon;
|
||||
}
|
||||
|
||||
public void setAppIcon(Drawable appIcon) {
|
||||
this.appIcon = appIcon;
|
||||
}
|
||||
|
||||
public boolean isSystemApp() {
|
||||
return isSystemApp;
|
||||
}
|
||||
|
||||
public void setSystemApp(boolean systemApp) {
|
||||
isSystemApp = systemApp;
|
||||
}
|
||||
|
||||
public boolean isEnabled() {
|
||||
return isEnabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
isEnabled = enabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AppInfo{" +
|
||||
"appName='" + appName + '\'' +
|
||||
", packageName='" + packageName + '\'' +
|
||||
", isSystemApp=" + isSystemApp +
|
||||
", isEnabled=" + isEnabled +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
125
configapp/src/main/java/com/jiqiu/configapp/AppListAdapter.java
Normal file
125
configapp/src/main/java/com/jiqiu/configapp/AppListAdapter.java
Normal file
@@ -0,0 +1,125 @@
|
||||
package com.jiqiu.configapp;
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.google.android.material.switchmaterial.SwitchMaterial;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 应用列表适配器
|
||||
*/
|
||||
public class AppListAdapter extends RecyclerView.Adapter<AppListAdapter.AppViewHolder> {
|
||||
|
||||
private List<AppInfo> appList;
|
||||
private List<AppInfo> filteredAppList;
|
||||
private OnAppToggleListener onAppToggleListener;
|
||||
|
||||
public interface OnAppToggleListener {
|
||||
void onAppToggle(AppInfo appInfo, boolean isEnabled);
|
||||
}
|
||||
|
||||
public AppListAdapter() {
|
||||
this.appList = new ArrayList<>();
|
||||
this.filteredAppList = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void setAppList(List<AppInfo> appList) {
|
||||
this.appList = appList;
|
||||
this.filteredAppList = new ArrayList<>(appList);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public void setOnAppToggleListener(OnAppToggleListener listener) {
|
||||
this.onAppToggleListener = listener;
|
||||
}
|
||||
|
||||
public void filterApps(String query, boolean hideSystemApps) {
|
||||
filteredAppList.clear();
|
||||
|
||||
for (AppInfo app : appList) {
|
||||
// 过滤系统应用
|
||||
if (hideSystemApps && app.isSystemApp()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 搜索过滤
|
||||
if (query == null || query.isEmpty() ||
|
||||
app.getAppName().toLowerCase().contains(query.toLowerCase()) ||
|
||||
app.getPackageName().toLowerCase().contains(query.toLowerCase())) {
|
||||
filteredAppList.add(app);
|
||||
}
|
||||
}
|
||||
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public AppViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.item_app, parent, false);
|
||||
return new AppViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull AppViewHolder holder, int position) {
|
||||
AppInfo appInfo = filteredAppList.get(position);
|
||||
holder.bind(appInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return filteredAppList.size();
|
||||
}
|
||||
|
||||
class AppViewHolder extends RecyclerView.ViewHolder {
|
||||
private ImageView appIcon;
|
||||
private TextView appName;
|
||||
private TextView packageName;
|
||||
private TextView systemAppLabel;
|
||||
private SwitchMaterial switchEnable;
|
||||
|
||||
public AppViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
appIcon = itemView.findViewById(R.id.app_icon);
|
||||
appName = itemView.findViewById(R.id.app_name);
|
||||
packageName = itemView.findViewById(R.id.package_name);
|
||||
systemAppLabel = itemView.findViewById(R.id.system_app_label);
|
||||
switchEnable = itemView.findViewById(R.id.switch_enable);
|
||||
}
|
||||
|
||||
public void bind(AppInfo appInfo) {
|
||||
appIcon.setImageDrawable(appInfo.getAppIcon());
|
||||
appName.setText(appInfo.getAppName());
|
||||
packageName.setText(appInfo.getPackageName());
|
||||
|
||||
// 显示系统应用标签
|
||||
if (appInfo.isSystemApp()) {
|
||||
systemAppLabel.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
systemAppLabel.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
// 设置开关状态
|
||||
switchEnable.setOnCheckedChangeListener(null); // 清除之前的监听器
|
||||
switchEnable.setChecked(appInfo.isEnabled());
|
||||
|
||||
// 设置开关监听器
|
||||
switchEnable.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||
appInfo.setEnabled(isChecked);
|
||||
if (onAppToggleListener != null) {
|
||||
onAppToggleListener.onAppToggle(appInfo, isChecked);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
166
configapp/src/main/java/com/jiqiu/configapp/AppListFragment.java
Normal file
166
configapp/src/main/java/com/jiqiu/configapp/AppListFragment.java
Normal file
@@ -0,0 +1,166 @@
|
||||
package com.jiqiu.configapp;
|
||||
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ProgressBar;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.google.android.material.textfield.TextInputEditText;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 应用列表Fragment
|
||||
*/
|
||||
public class AppListFragment extends Fragment implements AppListAdapter.OnAppToggleListener {
|
||||
|
||||
private RecyclerView recyclerView;
|
||||
private AppListAdapter adapter;
|
||||
private TextInputEditText searchEditText;
|
||||
private ProgressBar progressBar;
|
||||
|
||||
private List<AppInfo> allApps;
|
||||
private boolean hideSystemApps = false;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.fragment_app_list, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
initViews(view);
|
||||
setupRecyclerView();
|
||||
setupSearchView();
|
||||
loadApps();
|
||||
}
|
||||
|
||||
private void initViews(View view) {
|
||||
recyclerView = view.findViewById(R.id.recycler_view_apps);
|
||||
searchEditText = view.findViewById(R.id.search_edit_text);
|
||||
progressBar = view.findViewById(R.id.progress_bar);
|
||||
}
|
||||
|
||||
private void setupRecyclerView() {
|
||||
adapter = new AppListAdapter();
|
||||
adapter.setOnAppToggleListener(this);
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
recyclerView.setAdapter(adapter);
|
||||
}
|
||||
|
||||
private void setupSearchView() {
|
||||
searchEditText.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
filterApps(s.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {}
|
||||
});
|
||||
}
|
||||
|
||||
private void loadApps() {
|
||||
progressBar.setVisibility(View.VISIBLE);
|
||||
recyclerView.setVisibility(View.GONE);
|
||||
|
||||
new LoadAppsTask().execute();
|
||||
}
|
||||
|
||||
private void filterApps(String query) {
|
||||
if (adapter != null) {
|
||||
adapter.filterApps(query, hideSystemApps);
|
||||
}
|
||||
}
|
||||
|
||||
public void setHideSystemApps(boolean hideSystemApps) {
|
||||
this.hideSystemApps = hideSystemApps;
|
||||
filterApps(searchEditText.getText().toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAppToggle(AppInfo appInfo, boolean isEnabled) {
|
||||
// 这里可以保存应用的启用状态到配置文件或数据库
|
||||
// 暂时只是打印日志
|
||||
android.util.Log.d("AppListFragment",
|
||||
"App " + appInfo.getAppName() + " toggle: " + isEnabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步加载应用列表
|
||||
*/
|
||||
private class LoadAppsTask extends AsyncTask<Void, Void, List<AppInfo>> {
|
||||
|
||||
@Override
|
||||
protected List<AppInfo> doInBackground(Void... voids) {
|
||||
List<AppInfo> apps = new ArrayList<>();
|
||||
PackageManager pm = getContext().getPackageManager();
|
||||
|
||||
List<ApplicationInfo> installedApps = pm.getInstalledApplications(PackageManager.GET_META_DATA);
|
||||
|
||||
for (ApplicationInfo appInfo : installedApps) {
|
||||
try {
|
||||
String appName = pm.getApplicationLabel(appInfo).toString();
|
||||
String packageName = appInfo.packageName;
|
||||
boolean isSystemApp = (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
|
||||
|
||||
AppInfo app = new AppInfo(
|
||||
appName,
|
||||
packageName,
|
||||
pm.getApplicationIcon(appInfo),
|
||||
isSystemApp
|
||||
);
|
||||
|
||||
apps.add(app);
|
||||
} catch (Exception e) {
|
||||
// 忽略无法获取信息的应用
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// 按应用名称排序
|
||||
Collections.sort(apps, new Comparator<AppInfo>() {
|
||||
@Override
|
||||
public int compare(AppInfo o1, AppInfo o2) {
|
||||
return o1.getAppName().compareToIgnoreCase(o2.getAppName());
|
||||
}
|
||||
});
|
||||
|
||||
return apps;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(List<AppInfo> apps) {
|
||||
allApps = apps;
|
||||
adapter.setAppList(apps);
|
||||
|
||||
progressBar.setVisibility(View.GONE);
|
||||
recyclerView.setVisibility(View.VISIBLE);
|
||||
|
||||
// 应用当前的过滤设置
|
||||
filterApps(searchEditText.getText().toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,8 +7,17 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.graphics.Insets;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements SettingsFragment.OnSettingsChangeListener {
|
||||
|
||||
private BottomNavigationView bottomNavigationView;
|
||||
private AppListFragment appListFragment;
|
||||
private SettingsFragment settingsFragment;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -20,5 +29,61 @@ public class MainActivity extends AppCompatActivity {
|
||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
|
||||
return insets;
|
||||
});
|
||||
|
||||
initViews();
|
||||
setupBottomNavigation();
|
||||
|
||||
// 默认显示应用列表
|
||||
if (savedInstanceState == null) {
|
||||
showAppListFragment();
|
||||
}
|
||||
}
|
||||
|
||||
private void initViews() {
|
||||
bottomNavigationView = findViewById(R.id.bottom_navigation);
|
||||
}
|
||||
|
||||
private void setupBottomNavigation() {
|
||||
bottomNavigationView.setOnItemSelectedListener(item -> {
|
||||
int itemId = item.getItemId();
|
||||
if (itemId == R.id.navigation_apps) {
|
||||
showAppListFragment();
|
||||
return true;
|
||||
} else if (itemId == R.id.navigation_settings) {
|
||||
showSettingsFragment();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
private void showAppListFragment() {
|
||||
if (appListFragment == null) {
|
||||
appListFragment = new AppListFragment();
|
||||
}
|
||||
showFragment(appListFragment);
|
||||
}
|
||||
|
||||
private void showSettingsFragment() {
|
||||
if (settingsFragment == null) {
|
||||
settingsFragment = new SettingsFragment();
|
||||
settingsFragment.setOnSettingsChangeListener(this);
|
||||
}
|
||||
showFragment(settingsFragment);
|
||||
}
|
||||
|
||||
private void showFragment(Fragment fragment) {
|
||||
FragmentManager fragmentManager = getSupportFragmentManager();
|
||||
FragmentTransaction transaction = fragmentManager.beginTransaction();
|
||||
transaction.replace(R.id.nav_host_fragment, fragment);
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHideSystemAppsChanged(boolean hideSystemApps) {
|
||||
// 当设置改变时,通知应用列表Fragment更新过滤
|
||||
if (appListFragment != null) {
|
||||
appListFragment.setHideSystemApps(hideSystemApps);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.jiqiu.configapp;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.RadioGroup;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
/**
|
||||
* 设置Fragment
|
||||
*/
|
||||
public class SettingsFragment extends Fragment {
|
||||
|
||||
private static final String PREFS_NAME = "MyInjectorSettings";
|
||||
private static final String KEY_HIDE_SYSTEM_APPS = "hide_system_apps";
|
||||
|
||||
private RadioGroup radioGroupFilter;
|
||||
private RadioButton radioShowAll;
|
||||
private RadioButton radioHideSystem;
|
||||
|
||||
private SharedPreferences sharedPreferences;
|
||||
private OnSettingsChangeListener settingsChangeListener;
|
||||
|
||||
public interface OnSettingsChangeListener {
|
||||
void onHideSystemAppsChanged(boolean hideSystemApps);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.fragment_settings, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
initViews(view);
|
||||
initSharedPreferences();
|
||||
loadSettings();
|
||||
setupListeners();
|
||||
}
|
||||
|
||||
private void initViews(View view) {
|
||||
radioGroupFilter = view.findViewById(R.id.radio_group_filter);
|
||||
radioShowAll = view.findViewById(R.id.radio_show_all);
|
||||
radioHideSystem = view.findViewById(R.id.radio_hide_system);
|
||||
}
|
||||
|
||||
private void initSharedPreferences() {
|
||||
sharedPreferences = getContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
|
||||
}
|
||||
|
||||
private void loadSettings() {
|
||||
boolean hideSystemApps = sharedPreferences.getBoolean(KEY_HIDE_SYSTEM_APPS, false);
|
||||
|
||||
if (hideSystemApps) {
|
||||
radioHideSystem.setChecked(true);
|
||||
} else {
|
||||
radioShowAll.setChecked(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void setupListeners() {
|
||||
radioGroupFilter.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(RadioGroup group, int checkedId) {
|
||||
boolean hideSystemApps = (checkedId == R.id.radio_hide_system);
|
||||
|
||||
// 保存设置
|
||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||
editor.putBoolean(KEY_HIDE_SYSTEM_APPS, hideSystemApps);
|
||||
editor.apply();
|
||||
|
||||
// 通知设置变化
|
||||
if (settingsChangeListener != null) {
|
||||
settingsChangeListener.onHideSystemAppsChanged(hideSystemApps);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void setOnSettingsChangeListener(OnSettingsChangeListener listener) {
|
||||
this.settingsChangeListener = listener;
|
||||
}
|
||||
|
||||
public boolean isHideSystemApps() {
|
||||
return sharedPreferences.getBoolean(KEY_HIDE_SYSTEM_APPS, false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user