Issue #143 Run button

This commit is contained in:
Sergey Eremin
2017-04-25 03:21:23 +03:00
parent b3527d954c
commit dd9f9bfd18
3 changed files with 37 additions and 3 deletions

View File

@@ -4,6 +4,7 @@ import android.Manifest;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
@@ -48,12 +49,16 @@ public class DownloadOrInstallFragment extends DetailsFragment {
private void drawDownloadButton() {
Button downloadButton = (Button) activity.findViewById(R.id.download);
downloadButton.setText(apkPath.exists() ? R.string.details_install : R.string.details_download);
downloadButton.setEnabled(apkPath.exists() || app.isInPlayStore());
downloadButton.setText(getDownloadButtonText());
downloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (app.getVersionCode() == 0 && !(activity instanceof ManualDownloadActivity)) {
if (isLatestVersion()) {
Intent launchIntent = getLaunchIntent();
if (null != launchIntent) {
activity.startActivity(launchIntent);
}
} else if (app.getVersionCode() == 0 && !(activity instanceof ManualDownloadActivity)) {
activity.startActivity(new Intent(activity, ManualDownloadActivity.class));
} else if (checkPermission()) {
downloadOrInstall();
@@ -64,6 +69,33 @@ public class DownloadOrInstallFragment extends DetailsFragment {
});
}
private int getDownloadButtonText() {
int stringId = R.string.details_download;
if (isLatestVersion() && null != getLaunchIntent()) {
stringId = R.string.details_run;
} else if (apkPath.exists()) {
stringId = R.string.details_install;
}
return stringId;
}
private boolean isLatestVersion() {
try {
return activity.getPackageManager().getPackageInfo(app.getPackageName(), 0).versionCode == app.getVersionCode();
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
private Intent getLaunchIntent() {
Intent i = activity.getPackageManager().getLaunchIntentForPackage(app.getPackageName());
if (i == null) {
return null;
}
i.addCategory(Intent.CATEGORY_LAUNCHER);
return i;
}
public void unregisterReceivers() {
if (null != downloadReceiver) {
activity.unregisterReceiver(downloadReceiver);

View File

@@ -80,6 +80,7 @@
<string name="details_permissions">Разрешения:</string>
<string name="details_versionName">"Версия %1$s"</string>
<string name="details_versionName_updatable">"Версия %1$s → %2$s"</string>
<string name="details_run">Запустить</string>
<string name="details_download">Скачать</string>
<string name="details_install">Установить</string>
<string name="details_uninstall">Удалить</string>

View File

@@ -79,6 +79,7 @@
<string name="details_permissions">Permissions:</string>
<string name="details_versionName">"Version %1$s"</string>
<string name="details_versionName_updatable">"Version %1$s → %2$s"</string>
<string name="details_run">Run</string>
<string name="details_download">Download</string>
<string name="details_install">Install</string>
<string name="details_uninstall">Uninstall</string>