diff options
Diffstat (limited to 'app/src/main/java/moe/yuuta/workmode/MainActivity.kt')
-rw-r--r-- | app/src/main/java/moe/yuuta/workmode/MainActivity.kt | 116 |
1 files changed, 78 insertions, 38 deletions
diff --git a/app/src/main/java/moe/yuuta/workmode/MainActivity.kt b/app/src/main/java/moe/yuuta/workmode/MainActivity.kt index 1346f6d..587a86a 100644 --- a/app/src/main/java/moe/yuuta/workmode/MainActivity.kt +++ b/app/src/main/java/moe/yuuta/workmode/MainActivity.kt @@ -34,11 +34,10 @@ import moe.yuuta.workmode.async.* import moe.yuuta.workmode.gpl.GPL import moe.yuuta.workmode.suspend.AsyncSuspender import moe.yuuta.workmode.suspend.SuspendTile -import moe.yuuta.workmode.suspend.data.ListMode -import moe.yuuta.workmode.suspend.data.Status -import moe.yuuta.workmode.suspend.data.SuspendedStorage +import moe.yuuta.workmode.suspend.data.* import moe.yuuta.workmode.update.LifecycleUpdateChecker import moe.yuuta.workmode.utils.Utils +import java.util.concurrent.CompletableFuture import java.util.stream.Collectors class MainActivity : AppCompatActivity(), SwitchBar.OnSwitchChangeListener, View.OnClickListener, LicenseCheckerCallback, moe.yuuta.workmode.update.Callback, LifecycleUIUpdateReceiver.Callback { @@ -57,7 +56,7 @@ class MainActivity : AppCompatActivity(), SwitchBar.OnSwitchChangeListener, View private lateinit var mCheckUpdateObserver: LifecycleUpdateChecker - private val mStoppableGroup: StoppableGroup = StoppableGroup() + private lateinit var mApplyFuture: CompletableFuture<Unit> private var mSortDisplayStoppable: Stoppable? = null override fun onCreate(savedInstanceState: Bundle?) { @@ -85,6 +84,7 @@ class MainActivity : AppCompatActivity(), SwitchBar.OnSwitchChangeListener, View } override fun onSwitchChanged(switchView: Switch?, isChecked: Boolean) { + logger.d("onSwitchChanged $isChecked") SuspendedStorage.get(this).setStatus(if (isChecked) Status.ON else Status.OFF) scheduleApply() } @@ -93,31 +93,34 @@ class MainActivity : AppCompatActivity(), SwitchBar.OnSwitchChangeListener, View * Apply settings which are stored in SuspendedStorage to OS */ private fun scheduleApply() { - mStoppableGroup.add(AsyncSuspender(this).applyFromSettings(object : Callback<Unit> { - override fun onStart() { - setProgressUI(true) + if (::mApplyFuture.isInitialized && !mApplyFuture.isDone) { + mApplyFuture.cancel(true) + } + mApplyFuture = AsyncSuspender(this) + .applyFromSettings() + setProgressUI(true) + mApplyFuture + .exceptionally { + logger.e("Unable scheduleApply settings", it) + if (Setup.FABRIC_ENABLE) + Crashlytics.getInstance().core.logException(it) + Toast.makeText(this@MainActivity, R.string.error_apply, Toast.LENGTH_LONG).show() } - - override fun onStop(success: Boolean, result: Unit?, e: Throwable?) { + .thenRun { setProgressUI(false) displayUI() - if (!success) { - logger.e("Unable scheduleApply settings", e) - if (Setup.FABRIC_ENABLE) - Crashlytics.getInstance().core.logException(e) - Toast.makeText(this@MainActivity, R.string.error_apply, Toast.LENGTH_LONG).show() - } } - })) } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { when (requestCode) { RC_PICK -> { - if (resultCode == Activity.RESULT_OK && data != null && data.hasExtra(ApplicationPickerActivity.EXTRA_SELECTED_PACKAGE_NAME)) { - val newSet = data.getStringArrayExtra(ApplicationPickerActivity.EXTRA_SELECTED_PACKAGE_NAME).toSet() + if (resultCode == Activity.RESULT_OK && data != null && data.hasExtra(ApplicationPickerActivity.EXTRA_SELECTED_PACKAGES)) { + val newSet = data.getParcelableArrayListExtra<TransferableSuspendedApp>(ApplicationPickerActivity.EXTRA_SELECTED_PACKAGES)?.toSet() ?: setOf<TransferableSuspendedApp>() logger.d("AR() $newSet") - SuspendedStorage.get(this).setList(newSet) + SuspendedStorage.get(this).setList(newSet.stream() + .map { return@map it.trimToPersistable() } + .collect(Collectors.toList()).toSet()) scheduleApply() } } @@ -132,7 +135,9 @@ class MainActivity : AppCompatActivity(), SwitchBar.OnSwitchChangeListener, View } override fun onDestroy() { - mStoppableGroup.stop() + if (::mApplyFuture.isInitialized && !mApplyFuture.isDone) { + mApplyFuture.cancel(true) + } mAdapter.destroy() if (mSortDisplayStoppable != null) (mSortDisplayStoppable as Stoppable).stop() super.onDestroy() @@ -143,7 +148,10 @@ class MainActivity : AppCompatActivity(), SwitchBar.OnSwitchChangeListener, View */ private fun displayUI() { switchBar.removeOnSwitchChangeListener(this) - switchBar.isChecked = SuspendedStorage.get(this).getStatus() == Status.ON + val checked = SuspendedStorage.get(this).getStatus() == Status.ON + // logger.d("code $code B-C") + if (checked != switchBar.isChecked) switchBar.isChecked = checked + // logger.d("code $code C") switchBar.addOnSwitchChangeListener(this) tabLayout.removeOnTabSelectedListener(mSwitchListModeListener) tabLayout.getTabAt( @@ -158,19 +166,37 @@ class MainActivity : AppCompatActivity(), SwitchBar.OnSwitchChangeListener, View stoppable.stop() mSortDisplayStoppable = null } - mSortDisplayStoppable = Async.beginTask(object : Runnable<List<String>> { - override fun run(): List<String>? { + mSortDisplayStoppable = Async.beginTask(object : Runnable<List<PersistableSuspendedApp>> { + override fun run(): List<PersistableSuspendedApp>? { val sCollator = java.text.Collator.getInstance() return SuspendedStorage.get(this@MainActivity).getList() .stream() .sorted { o1, o2 -> - return@sorted sCollator.compare(packageManager.getApplicationLabel(packageManager.getApplicationInfo(o1, 0)).toString() - , packageManager.getApplicationLabel(packageManager.getApplicationInfo(o2, 0))) + // TODO + val canSafelyLoadAppInfoForO1 = Utils.canSafelyLoadAppInfo(o1, this@MainActivity) + val canSafelyLoadAppInfoForO2 = Utils.canSafelyLoadAppInfo(o2, this@MainActivity) + if (!canSafelyLoadAppInfoForO1 || + !canSafelyLoadAppInfoForO2) { + if (o1.userId > o2.userId) { + return@sorted 1 + } else if (o1.userId < o2.userId) { + return@sorted -1 + } else { + return@sorted 0 + } + } + if ((canSafelyLoadAppInfoForO1 && !canSafelyLoadAppInfoForO2) || + (!canSafelyLoadAppInfoForO1 && canSafelyLoadAppInfoForO2)) { + // A unsafe app is comparing to a safe app, just put the unsafe one in under the safe one. + return@sorted 1 + } + return@sorted sCollator.compare(packageManager.getApplicationLabel(packageManager.getApplicationInfo(o1.packageName, 0)).toString() + , packageManager.getApplicationLabel(packageManager.getApplicationInfo(o2.packageName, 0))) } .collect(Collectors.toList()) } - }, object : Callback<List<String>> { - override fun onStop(success: Boolean, result: List<String>?, e: Throwable?) { + }, object : Callback<List<PersistableSuspendedApp>> { + override fun onStop(success: Boolean, result: List<PersistableSuspendedApp>?, e: Throwable?) { if (result != null) { val diff = DiffUtil.calculateDiff(object : DiffUtil.Callback() { override fun getOldListSize(): Int = mAdapter.itemCount @@ -309,9 +335,15 @@ class MainActivity : AppCompatActivity(), SwitchBar.OnSwitchChangeListener, View if (v == null) return when (v.id) { R.id.fab_add -> { + val selected = arrayListOf<TransferableSuspendedApp>() + selected.addAll(SuspendedStorage.get(this).getList().stream() + .map { + return@map it.copyToSimpleTransferableInfo() + } + .collect(Collectors.toList())) startActivityForResult(Intent(this, ApplicationPickerActivity::class.java) - .putExtra(ApplicationPickerActivity.EXTRA_SELECTED_PACKAGE_NAME, - SuspendedStorage.get(this).getList().toTypedArray()), RC_PICK) + .putExtra(ApplicationPickerActivity.EXTRA_SELECTED_PACKAGES, + selected), RC_PICK) } } } @@ -367,7 +399,7 @@ private class Adapter : RecyclerView.Adapter<Adapter.VH>() { private val mIconMemoryCaches: LruCache<String, Drawable> = LruCache(Runtime.getRuntime().maxMemory().toInt() / 5) private val mStoppableGroup: StoppableGroup = StoppableGroup() - internal var data: List<String> = listOf() + internal var data: List<PersistableSuspendedApp> = listOf() override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH = VH(LayoutInflater.from(parent.context).inflate(R.layout.item_application, parent, false)) @@ -376,23 +408,31 @@ private class Adapter : RecyclerView.Adapter<Adapter.VH>() { override fun onBindViewHolder(holder: VH, position: Int) { val context = holder.itemView.context - val packageName = data[position] - val icon = getIconFromMemoryCache(packageName) - if (icon != null) { - holder.icon.setImageDrawable(icon) - } else { - loadIcon(packageName, holder.itemView.context, holder.icon) + val packageInfo = data[position] + // TODO + if (Utils.canSafelyLoadAppInfo(packageInfo, context)) { + val icon = getIconFromMemoryCache(packageInfo.packageName) + if (icon != null) { + holder.icon.setImageDrawable(icon) + } else { + loadIcon(packageInfo.packageName, holder.itemView.context, holder.icon) + } } - holder.title.text = context + holder.title.text = if (Utils.canSafelyLoadAppInfo(packageInfo, context)) + context .packageManager .getApplicationLabel( - context.packageManager.getApplicationInfo(packageName, 0) + context.packageManager.getApplicationInfo(packageInfo.packageName, 0) ) + else + packageInfo.packageName + holder.summary.text = context.getString(R.string.app_list_user_template, packageInfo.userId) } class VH(itemView: View) : RecyclerView.ViewHolder(itemView) { internal val icon: ImageView = itemView.findViewById(android.R.id.icon) internal val title: TextView = itemView.findViewById(android.R.id.title) + internal val summary: TextView = itemView.findViewById(android.R.id.summary) } private fun addDrawableToMemoryCache(pkg: String?, icon: Drawable) { |