-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move out self updating outside of playstore variant
- Loading branch information
Showing
3 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
app/src/app/java/com/mxt/anitrend/base/custom/activity/ActivityDelegateExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.mxt.anitrend.base.custom.activity | ||
|
||
import android.content.Context | ||
import android.view.Menu | ||
import androidx.fragment.app.FragmentActivity | ||
import timber.log.Timber | ||
import com.mxt.anitrend.view.activity.index.MainActivity | ||
|
||
private fun Context.unsupportedFeature() { | ||
Timber.i("$packageName does not support checking updates, migrate to play services") | ||
} | ||
|
||
private fun FragmentActivity.onLatestUpdateInstalled() { | ||
unsupportedFeature() | ||
} | ||
|
||
private fun FragmentActivity.onUpdateChecked(silent: Boolean, menuItems: Menu) { | ||
unsupportedFeature() | ||
} | ||
|
||
fun FragmentActivity.launchUpdateWorker(menuItems: Menu) { | ||
unsupportedFeature() | ||
} | ||
|
||
fun MainActivity.checkUpdate() { | ||
unsupportedFeature() | ||
} |
126 changes: 126 additions & 0 deletions
126
app/src/github/java/com/mxt/anitrend/base/custom/activity/ActivityDelegateExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package com.mxt.anitrend.base.custom.activity | ||
|
||
import android.Manifest | ||
import android.content.Context | ||
import android.content.pm.PackageManager | ||
import android.view.Menu | ||
import android.view.View | ||
import android.widget.TextView | ||
import android.widget.Toast | ||
import androidx.core.app.ActivityCompat | ||
import androidx.core.content.ContextCompat | ||
import androidx.fragment.app.FragmentActivity | ||
import androidx.work.WorkInfo | ||
import androidx.work.WorkManager | ||
import com.afollestad.materialdialogs.DialogAction | ||
import com.mxt.anitrend.R | ||
import com.mxt.anitrend.base.interfaces.event.BottomSheetChoice | ||
import com.mxt.anitrend.service.DownloaderService | ||
import com.mxt.anitrend.util.DialogUtil | ||
import com.mxt.anitrend.util.KeyUtil | ||
import com.mxt.anitrend.util.NotifyUtil | ||
import timber.log.Timber | ||
import com.mxt.anitrend.view.activity.index.MainActivity | ||
import com.mxt.anitrend.view.sheet.BottomSheetMessage | ||
|
||
private fun FragmentActivity.onLatestUpdateInstalled() { | ||
NotifyUtil.createAlerter( | ||
this, | ||
getString(R.string.title_update_infodadat), | ||
getString(R.string.app_no_date), | ||
R.drawable.ic_cloud_done_white_24dp, | ||
R.color.colorStateGreen | ||
) | ||
} | ||
|
||
private fun MainActivity.onUpdateChecked(silent: Boolean, menuItems: Menu) { | ||
val remoteVersion = presenter.database.remoteVersion | ||
|
||
if (remoteVersion != null) { | ||
if (remoteVersion.isNewerVersion) { | ||
// If a new version of the application is available on GitHub | ||
val mAppUpdateWidget = menuItems.findItem(R.id.nav_check_update) | ||
.actionView.findViewById<TextView>(R.id.app_update_info) | ||
mAppUpdateWidget.text = getString(R.string.app_update, remoteVersion.version) | ||
mAppUpdateWidget.visibility = View.VISIBLE | ||
} else if (!silent) { | ||
onLatestUpdateInstalled() | ||
} | ||
} | ||
} | ||
|
||
fun MainActivity.launchUpdateWorker(menuItems: Menu) { | ||
WorkManager.getInstance(this) | ||
.getWorkInfosByTagLiveData( | ||
KeyUtil.WorkUpdaterId | ||
).observe(this) { workInfoList -> | ||
workInfoList.firstOrNull { workInfo -> | ||
workInfo.state == WorkInfo.State.SUCCEEDED | ||
}?.run { | ||
onUpdateChecked( | ||
outputData.getBoolean( | ||
KeyUtil.WorkUpdaterSilentId, | ||
false | ||
), | ||
menuItems | ||
) | ||
} | ||
} | ||
} | ||
|
||
fun MainActivity.checkUpdate() { | ||
when (ContextCompat.checkSelfPermission( | ||
applicationContext, | ||
Manifest.permission.WRITE_EXTERNAL_STORAGE | ||
)) { | ||
PackageManager.PERMISSION_GRANTED -> { | ||
mBottomSheet = BottomSheetMessage.Builder() | ||
.setText(R.string.drawer_update_text) | ||
.setTitle(R.string.drawer_update_title) | ||
.setPositiveText(R.string.Yes) | ||
.setNegativeText(R.string.No) | ||
.buildWithCallback(object : BottomSheetChoice { | ||
override fun onPositiveButton() { | ||
val versionBase = presenter.database.remoteVersion | ||
if (versionBase != null && versionBase.isNewerVersion) | ||
DownloaderService.downloadNewVersion( | ||
this@checkUpdate, | ||
versionBase | ||
) | ||
else presenter.checkForUpdates(false) | ||
} | ||
|
||
override fun onNegativeButton() { | ||
|
||
} | ||
}) | ||
showBottomSheet() | ||
} | ||
PackageManager.PERMISSION_DENIED -> if (ActivityCompat.shouldShowRequestPermissionRationale( | ||
this, | ||
Manifest.permission.WRITE_EXTERNAL_STORAGE | ||
) | ||
) | ||
DialogUtil.createMessage( | ||
this, | ||
R.string.title_permission_write, | ||
R.string.text_permission_write | ||
) { _, which -> | ||
when (which) { | ||
DialogAction.POSITIVE -> ActivityCompat.requestPermissions( | ||
this, | ||
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), | ||
ActivityBase.REQUEST_PERMISSION | ||
) | ||
DialogAction.NEGATIVE -> NotifyUtil.makeText( | ||
this, | ||
R.string.canceled_by_user, | ||
Toast.LENGTH_SHORT | ||
).show() | ||
else -> {} | ||
} | ||
} | ||
else | ||
requestPermissionIfMissing(Manifest.permission.WRITE_EXTERNAL_STORAGE) | ||
} | ||
} |
File renamed without changes.