|
1 | 1 | package com.ackee.versioupdatehandler; |
2 | 2 |
|
| 3 | +import android.app.Dialog; |
| 4 | +import android.content.DialogInterface; |
| 5 | +import android.content.Intent; |
| 6 | +import android.net.Uri; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.support.annotation.NonNull; |
| 9 | +import android.support.v4.app.DialogFragment; |
| 10 | +import android.support.v4.app.FragmentManager; |
| 11 | +import android.support.v7.app.AlertDialog; |
3 | 12 | import android.util.Log; |
4 | 13 |
|
5 | 14 | import com.ackee.versioupdatehandler.model.VersionStatus; |
6 | 15 | import com.ackee.versioupdatehandler.model.VersionsConfiguration; |
7 | 16 |
|
8 | 17 | import rx.Single; |
| 18 | +import rx.android.schedulers.AndroidSchedulers; |
| 19 | +import rx.functions.Action1; |
9 | 20 | import rx.functions.Func1; |
| 21 | +import rx.schedulers.Schedulers; |
10 | 22 |
|
11 | 23 |
|
12 | 24 | /** |
@@ -46,4 +58,80 @@ private VersionStatus resolveStatus(int version, VersionsConfiguration configura |
46 | 58 | " current version: " + configuration.minimalVersion() + " Status resolved: " + status); |
47 | 59 | return status; |
48 | 60 | } |
| 61 | + |
| 62 | + public void checkVersionStatusAndOpenDefault(final int version, final FragmentManager fragmentManager) { |
| 63 | + checkVersionStatus(version) |
| 64 | + .subscribeOn(Schedulers.newThread()) |
| 65 | + .observeOn(AndroidSchedulers.mainThread()) |
| 66 | + .subscribe(new Action1<VersionStatus>() { |
| 67 | + @Override |
| 68 | + public void call(VersionStatus versionStatus) { |
| 69 | + if (versionStatus != VersionStatus.UP_TO_DATE) { |
| 70 | + showDialog(versionStatus == VersionStatus.UPDATE_REQUIRED, fragmentManager); |
| 71 | + } |
| 72 | + } |
| 73 | + }, new Action1<Throwable>() { |
| 74 | + @Override |
| 75 | + public void call(Throwable throwable) { |
| 76 | + throwable.printStackTrace(); |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Show dialog with update info |
| 83 | + * |
| 84 | + * @param forceUpdate indicator if update is mandatory |
| 85 | + */ |
| 86 | + private void showDialog(boolean forceUpdate, FragmentManager fragmentManager) { |
| 87 | + UpdateDialog.newInstance(forceUpdate).show(fragmentManager, UpdateDialog.class.getName()); |
| 88 | + } |
| 89 | + |
| 90 | + public static class UpdateDialog extends DialogFragment { |
| 91 | + |
| 92 | + private static final String FORCE_UPDATE_KEY = "force_update"; |
| 93 | + |
| 94 | + public static UpdateDialog newInstance(boolean forceUpdate) { |
| 95 | + Bundle args = new Bundle(); |
| 96 | + args.putBoolean(FORCE_UPDATE_KEY, forceUpdate); |
| 97 | + UpdateDialog updateDialog = new UpdateDialog(); |
| 98 | + updateDialog.setArguments(args); |
| 99 | + return updateDialog; |
| 100 | + } |
| 101 | + |
| 102 | + @NonNull |
| 103 | + @Override |
| 104 | + public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 105 | + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
| 106 | + builder.setTitle("Update app"); |
| 107 | + builder.setMessage("Your application is outdated. Please update to the newest version"); |
| 108 | + final boolean isForceUpdate = getArguments().getBoolean(FORCE_UPDATE_KEY, false); |
| 109 | + builder.setPositiveButton("Update", new DialogInterface.OnClickListener() { |
| 110 | + @Override |
| 111 | + public void onClick(DialogInterface dialog, int which) { |
| 112 | + if (isForceUpdate) { |
| 113 | + getActivity().finish(); |
| 114 | + } |
| 115 | + final String appPackageName = getActivity().getPackageName(); // getPackageName() from Context or Activity object |
| 116 | + try { |
| 117 | + startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName))); |
| 118 | + } catch (android.content.ActivityNotFoundException anfe) { |
| 119 | + startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName))); |
| 120 | + } |
| 121 | + } |
| 122 | + }); |
| 123 | + builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
| 124 | + @Override |
| 125 | + public void onClick(DialogInterface dialog, int which) { |
| 126 | + if (isForceUpdate) { |
| 127 | + getActivity().moveTaskToBack(true); |
| 128 | + } |
| 129 | + } |
| 130 | + }); |
| 131 | + return builder.create(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
49 | 135 | } |
| 136 | + |
| 137 | + |
0 commit comments