|
| 1 | +package app.gwo.wechat.docuiproxy; |
| 2 | + |
| 3 | +import android.app.ActionBar; |
| 4 | +import android.content.Intent; |
| 5 | +import android.content.pm.PackageManager; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.view.Menu; |
| 8 | +import android.view.MenuItem; |
| 9 | +import android.view.View; |
| 10 | +import android.widget.ProgressBar; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import androidx.annotation.NonNull; |
| 16 | +import androidx.annotation.Nullable; |
| 17 | +import androidx.recyclerview.widget.RecyclerView; |
| 18 | +import app.gwo.wechat.docuiproxy.adapter.PackagesSelectorAdapter; |
| 19 | +import app.gwo.wechat.docuiproxy.compat.CollectionsCompat; |
| 20 | +import app.gwo.wechat.docuiproxy.model.CheckableAppInfo; |
| 21 | +import app.gwo.wechat.docuiproxy.util.Settings; |
| 22 | +import io.reactivex.Flowable; |
| 23 | +import io.reactivex.Single; |
| 24 | +import io.reactivex.android.schedulers.AndroidSchedulers; |
| 25 | +import io.reactivex.disposables.CompositeDisposable; |
| 26 | +import io.reactivex.schedulers.Schedulers; |
| 27 | + |
| 28 | +import static app.gwo.wechat.docuiproxy.Constants.WECHAT_PACKAGE_NAME; |
| 29 | +import static java.util.Objects.requireNonNull; |
| 30 | + |
| 31 | +public final class PackagesSelectorActivity extends BaseActivity { |
| 32 | + |
| 33 | + public static final String EXTRA_SELECTED = PackagesSelectorActivity.class.getName() + |
| 34 | + ".extra.SELECTED"; |
| 35 | + public static final String EXTRA_PACKAGES_RESULT = PackagesSelectorActivity.class.getName() + |
| 36 | + ".extra.PACKAGES_RESULT"; |
| 37 | + |
| 38 | + public static List<String> getResult(@NonNull Intent intent) { |
| 39 | + return intent.getStringArrayListExtra(EXTRA_PACKAGES_RESULT); |
| 40 | + } |
| 41 | + |
| 42 | + private RecyclerView mRecyclerView; |
| 43 | + private ProgressBar mProgressBar; |
| 44 | + |
| 45 | + private PackagesSelectorAdapter mAdapter = new PackagesSelectorAdapter(); |
| 46 | + |
| 47 | + @Nullable |
| 48 | + private List<String> mLastSelectedPackages = null; |
| 49 | + |
| 50 | + private final CompositeDisposable mDisposables = new CompositeDisposable(); |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 54 | + super.onCreate(savedInstanceState); |
| 55 | + setContentView(R.layout.activity_packages_selector_content); |
| 56 | + |
| 57 | + final Intent intent = getIntent(); |
| 58 | + if (intent != null) { |
| 59 | + mLastSelectedPackages = intent.getStringArrayListExtra(EXTRA_SELECTED); |
| 60 | + } |
| 61 | + |
| 62 | + final ActionBar actionBar = requireNonNull(getActionBar()); |
| 63 | + actionBar.setDisplayHomeAsUpEnabled(true); |
| 64 | + actionBar.setHomeAsUpIndicator(R.drawable.ic_close_black_24dp); |
| 65 | + |
| 66 | + mRecyclerView = findViewById(android.R.id.list); |
| 67 | + mProgressBar = findViewById(android.R.id.progress); |
| 68 | + |
| 69 | + mAdapter.onRestoreInstanceState(savedInstanceState); |
| 70 | + mRecyclerView.setAdapter(mAdapter); |
| 71 | + |
| 72 | + if (savedInstanceState == null) { |
| 73 | + loadAppInfoListAsync(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + protected void onDestroy() { |
| 79 | + super.onDestroy(); |
| 80 | + mDisposables.clear(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected void onSaveInstanceState(Bundle outState) { |
| 85 | + super.onSaveInstanceState(outState); |
| 86 | + mAdapter.onSaveInstanceState(outState); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 91 | + getMenuInflater().inflate(R.menu.menu_packages_selector, menu); |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 97 | + if (R.id.action_done == item.getItemId()) { |
| 98 | + done(); |
| 99 | + return true; |
| 100 | + } |
| 101 | + return super.onOptionsItemSelected(item); |
| 102 | + } |
| 103 | + |
| 104 | + private void loadAppInfoListAsync() { |
| 105 | + mProgressBar.setVisibility(View.VISIBLE); |
| 106 | + mDisposables.clear(); |
| 107 | + mDisposables.add(Single.fromCallable(this::loadAppInfoList) |
| 108 | + .toFlowable() |
| 109 | + .flatMap(Flowable::fromIterable) |
| 110 | + .sorted() |
| 111 | + .toList() |
| 112 | + .subscribeOn(Schedulers.io()) |
| 113 | + .observeOn(AndroidSchedulers.mainThread()) |
| 114 | + .subscribe((res, err) -> { |
| 115 | + mProgressBar.setVisibility(View.GONE); |
| 116 | + if (err != null) { |
| 117 | + err.printStackTrace(); |
| 118 | + return; |
| 119 | + } |
| 120 | + mAdapter.updateData(res); |
| 121 | + if (mLastSelectedPackages != null) { |
| 122 | + final List<String> packages = new ArrayList<>(mLastSelectedPackages); |
| 123 | + if (!packages.contains(WECHAT_PACKAGE_NAME)) { |
| 124 | + packages.add(WECHAT_PACKAGE_NAME); |
| 125 | + } |
| 126 | + mAdapter.updateCheckedPackages(packages); |
| 127 | + } |
| 128 | + }) |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + private List<CheckableAppInfo> loadAppInfoList() { |
| 133 | + final PackageManager pm = getPackageManager(); |
| 134 | + final List<String> checkedPacks = Settings.getInstance().getHandledApps(); |
| 135 | + return CollectionsCompat.mapToList(pm.getInstalledApplications(0), item -> { |
| 136 | + final CheckableAppInfo cai = CheckableAppInfo.build(this, item); |
| 137 | + if (WECHAT_PACKAGE_NAME.equals(cai.getPackageName())) { |
| 138 | + cai.setChecked(true); |
| 139 | + cai.setCheckable(false); |
| 140 | + } else { |
| 141 | + cai.setChecked(CollectionsCompat.anyMatch( |
| 142 | + checkedPacks, pack -> pack.equals(cai.getPackageName()))); |
| 143 | + } |
| 144 | + return cai; |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + private void done() { |
| 149 | + Intent resultIntent = new Intent(); |
| 150 | + resultIntent.putStringArrayListExtra(EXTRA_PACKAGES_RESULT, |
| 151 | + Flowable.fromIterable(mAdapter.getCheckedData()) |
| 152 | + .filter(item -> !WECHAT_PACKAGE_NAME.equals(item.getPackageName())) |
| 153 | + .map(CheckableAppInfo::getPackageName) |
| 154 | + .toList() |
| 155 | + .map(ArrayList::new) |
| 156 | + .blockingGet() |
| 157 | + ); |
| 158 | + setResult(RESULT_OK, resultIntent); |
| 159 | + finish(); |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments