diff --git a/build/android/src/main/AndroidManifest.xml b/build/android/src/main/AndroidManifest.xml index f851a6c47..caff6449f 100644 --- a/build/android/src/main/AndroidManifest.xml +++ b/build/android/src/main/AndroidManifest.xml @@ -2,12 +2,14 @@ + + - + + + + + + + + + + \ No newline at end of file diff --git a/build/android/src/main/java/mobi/MultiCraft/MainActivity.java b/build/android/src/main/java/mobi/MultiCraft/MainActivity.java index 6d2beafa7..85e760944 100644 --- a/build/android/src/main/java/mobi/MultiCraft/MainActivity.java +++ b/build/android/src/main/java/mobi/MultiCraft/MainActivity.java @@ -2,12 +2,14 @@ package mobi.MultiCraft; import android.Manifest; import android.app.Activity; -import android.app.ProgressDialog; +import android.app.ActivityManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; +import android.graphics.Bitmap; +import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.os.Build; @@ -35,8 +37,10 @@ import java.util.Arrays; import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE; import static mobi.MultiCraft.PreferencesHelper.TAG_BUILD_NUMBER; import static mobi.MultiCraft.PreferencesHelper.TAG_LAUNCH_TIMES; +import static mobi.MultiCraft.PreferencesHelper.TAG_SHORTCUT_CREATED; import static mobi.MultiCraft.PreferencesHelper.getBuildNumber; import static mobi.MultiCraft.PreferencesHelper.getLaunchTimes; +import static mobi.MultiCraft.PreferencesHelper.isCreateShortcut; import static mobi.MultiCraft.PreferencesHelper.isRestored; import static mobi.MultiCraft.PreferencesHelper.loadSettings; import static mobi.MultiCraft.PreferencesHelper.saveSettings; @@ -44,6 +48,7 @@ import static mobi.MultiCraft.PreferencesHelper.saveSettings; public class MainActivity extends Activity implements WVersionManager.ActivityListener { public final static int REQUEST_CODE = 104; private final static String TAG = "Error"; + private final static String CREATE_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT"; private final static String FILES = Environment.getExternalStorageDirectory() + "/Files.zip"; private final static String WORLDS = Environment.getExternalStorageDirectory() + "/worlds.zip"; private final static String GAMES = Environment.getExternalStorageDirectory() + "/games.zip"; @@ -53,10 +58,10 @@ public class MainActivity extends Activity implements WVersionManager.ActivityLi private final static int ALL_PERMISSIONS_RESULT = 102; private static final String UPDATE_LINK = "https://raw.githubusercontent.com/MoNTE48/MultiCraft-links/master/ver.txt"; - private ProgressDialog mProgressDialog; private String dataFolder = "/Android/data/mobi.MultiCraft/files/"; private String unzipLocation = Environment.getExternalStorageDirectory() + dataFolder; private ProgressBar mProgressBar; + private ProgressBar mProgressBarIndeterminate; private TextView mLoading; private ImageView iv; private WVersionManager versionManager = null; @@ -65,7 +70,10 @@ public class MainActivity extends Activity implements WVersionManager.ActivityLi private BroadcastReceiver myReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - int progress = intent.getIntExtra(UnzipService.ACTION_PROGRESS, 0); + int progress = 0; + if (intent != null) { + progress = intent.getIntExtra(UnzipService.ACTION_PROGRESS, 0); + } if (progress >= 0) { mProgressBar.setVisibility(View.VISIBLE); mProgressBar.setProgress(progress); @@ -104,7 +112,6 @@ public class MainActivity extends Activity implements WVersionManager.ActivityLi @Override protected void onDestroy() { super.onDestroy(); - dismissProgressDialog(); unregisterReceiver(myReceiver); } @@ -154,6 +161,32 @@ public class MainActivity extends Activity implements WVersionManager.ActivityLi } //interface + private void addShortcut() { + ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); + int size = activityManager.getLauncherLargeIconSize(); + try { + Drawable icon = getPackageManager().getApplicationIcon(getPackageName()); + Bitmap shortcutIconBitmap = ((BitmapDrawable) icon).getBitmap(); + Bitmap temp; + if (shortcutIconBitmap.getWidth() == size && shortcutIconBitmap.getHeight() == size) + temp = shortcutIconBitmap; + else + temp = Bitmap.createScaledBitmap(shortcutIconBitmap, size, size, true); + saveSettings(TAG_SHORTCUT_CREATED, false); + Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class); + shortcutIntent.setAction(Intent.ACTION_MAIN); + Intent addIntent = new Intent(); + addIntent.putExtra("duplicate", false); + addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); + addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); + addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, temp); + addIntent.setAction(CREATE_SHORTCUT); + getApplicationContext().sendBroadcast(addIntent); + } catch (PackageManager.NameNotFoundException e) { + Log.e(TAG, "Shortcut cannot be created"); + } + } + private void addImageView(int pos) { int marginTop = pos == 0 ? 48 : 288; RelativeLayout rl = (RelativeLayout) findViewById(R.id.activity_main); @@ -169,6 +202,7 @@ public class MainActivity extends Activity implements WVersionManager.ActivityLi private void hideViews() { mProgressBar.setVisibility(View.GONE); + mProgressBarIndeterminate.setVisibility(View.GONE); iv.setVisibility(View.GONE); mLoading.setVisibility(View.GONE); } @@ -188,25 +222,12 @@ public class MainActivity extends Activity implements WVersionManager.ActivityLi } } - private void showSpinnerDialog(int message) { - if (mProgressDialog == null) { - mProgressDialog = new ProgressDialog(MainActivity.this); - mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); - mProgressDialog.setCancelable(false); - } - mProgressDialog.setMessage(getString(message)); - mProgressDialog.show(); - } - - private void dismissProgressDialog() { - if (mProgressDialog != null && mProgressDialog.isShowing()) { - mProgressDialog.dismiss(); - } - } - public void init() { RateMe.onStart(this); + if (isCreateShortcut()) + addShortcut(); mProgressBar = (ProgressBar) findViewById(R.id.PB1); + mProgressBarIndeterminate = (ProgressBar) findViewById(R.id.PB2); mLoading = (TextView) findViewById(R.id.tv_progress_circle); Drawable draw = ContextCompat.getDrawable(this, R.drawable.custom_progress_bar); mProgressBar.setProgressDrawable(draw); @@ -337,7 +358,6 @@ public class MainActivity extends Activity implements WVersionManager.ActivityLi deleteZip(FILES, WORLDS, GAMES); Intent intent = new Intent(this, BillingActivity.class); startActivityForResult(intent, REQUEST_CODE); - } @@ -398,7 +418,9 @@ public class MainActivity extends Activity implements WVersionManager.ActivityLi @Override protected void onPreExecute() { super.onPreExecute(); - showSpinnerDialog(R.string.rm_old); + mProgressBarIndeterminate.setVisibility(View.VISIBLE); + mLoading.setVisibility(View.VISIBLE); + mLoading.setText(R.string.rm_old); } @Override @@ -410,11 +432,11 @@ public class MainActivity extends Activity implements WVersionManager.ActivityLi return null; } + @Override protected void onPostExecute(Void result) { if (isFinishing()) return; - dismissProgressDialog(); if (unzipLocation.equals(location)) { new CopyZip().execute(FILES, WORLDS, GAMES); } else { @@ -428,12 +450,6 @@ public class MainActivity extends Activity implements WVersionManager.ActivityLi private class CopyZip extends AsyncTask { String[] zips; - @Override - protected void onPreExecute() { - mProgressBar.setVisibility(View.VISIBLE); - mLoading.setVisibility(View.VISIBLE); - } - @Override protected String doInBackground(String... params) { zips = params; @@ -444,8 +460,11 @@ public class MainActivity extends Activity implements WVersionManager.ActivityLi } + @Override protected void onPostExecute(String result) { + mLoading.setText(R.string.loading); + mProgressBarIndeterminate.setVisibility(View.GONE); try { startUnzipService(zips); } catch (IOException e) { diff --git a/build/android/src/main/java/mobi/MultiCraft/PreferencesHelper.java b/build/android/src/main/java/mobi/MultiCraft/PreferencesHelper.java index 9c93d35b2..b95fd6e76 100644 --- a/build/android/src/main/java/mobi/MultiCraft/PreferencesHelper.java +++ b/build/android/src/main/java/mobi/MultiCraft/PreferencesHelper.java @@ -4,16 +4,22 @@ import android.content.Context; import android.content.SharedPreferences; class PreferencesHelper { + static final String TAG_SHORTCUT_CREATED = "createShortcut"; static final String TAG_BUILD_NUMBER = "buildNumber"; static final String TAG_LAUNCH_TIMES = "launchTimes"; static final String TAG_RESTORE_BACKUP = "restoredFromBackup"; private static final String SETTINGS = "settings"; private static final String TAG_DISABLED_ADS = "disabledADS"; private static String buildNumber; + private static boolean createShortcut; private static SharedPreferences settings; private static boolean disabledADS; + static boolean isCreateShortcut() { + return createShortcut; + } + static boolean isAdsDisabled() { return disabledADS; } @@ -37,6 +43,7 @@ class PreferencesHelper { static void loadSettings(final Context context) { settings = context.getSharedPreferences(SETTINGS, Context.MODE_PRIVATE); + createShortcut = settings.getBoolean(TAG_SHORTCUT_CREATED, true); buildNumber = settings.getString(TAG_BUILD_NUMBER, "0"); disabledADS = settings.getBoolean(TAG_DISABLED_ADS, false); } diff --git a/build/android/src/main/java/mobi/MultiCraft/RateMe.java b/build/android/src/main/java/mobi/MultiCraft/RateMe.java index b6c1fffcd..a451b2799 100644 --- a/build/android/src/main/java/mobi/MultiCraft/RateMe.java +++ b/build/android/src/main/java/mobi/MultiCraft/RateMe.java @@ -1,5 +1,6 @@ package mobi.MultiCraft; +import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; @@ -14,6 +15,7 @@ import android.util.Log; import android.view.View; import android.widget.RatingBar; +import java.lang.ref.WeakReference; import java.util.Date; class RateMe { @@ -32,11 +34,14 @@ class RateMe { private static boolean mOptOut = false; private static Callback sCallback = null; + private static WeakReference mainActivityRef = null; + static void setCallback(Callback callback) { sCallback = callback; } static void onStart(Context context) { + mainActivityRef = new WeakReference<>((Activity) context); SharedPreferences pref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); Editor editor = pref.edit(); // If it is the first launch, save the date in shared preference. @@ -112,7 +117,13 @@ class RateMe { clearSharedPreferences(context); } }); - dialog.show(); + if (mainActivityRef.get() != null && !mainActivityRef.get().isFinishing()) { + dialog.show(); + } else { + if (sCallback != null) { + sCallback.onNegative(); + } + } } private static void clearSharedPreferences(Context context) { diff --git a/build/android/src/main/res/layout/activity_main.xml b/build/android/src/main/res/layout/activity_main.xml index 034c14876..e0ba7ae20 100644 --- a/build/android/src/main/res/layout/activity_main.xml +++ b/build/android/src/main/res/layout/activity_main.xml @@ -16,13 +16,24 @@ android:max="100" android:visibility="gone" /> + + diff --git a/build/android/src/main/res/values-ru/strings.xml b/build/android/src/main/res/values-ru/strings.xml index a4ecdbd4f..ee357db7a 100644 --- a/build/android/src/main/res/values-ru/strings.xml +++ b/build/android/src/main/res/values-ru/strings.xml @@ -6,7 +6,6 @@ Загрузка… Загрузка MultiCraft Осталось меньше минуты… - Для корректной работы, игре требуется разрешение записывать в память устройтсва. Знание местоположения обеспечивает Вам лучшее взаимодействие с игрой @@ -21,6 +20,7 @@ Игнорировать Позже Обновить + Сообщение \ No newline at end of file diff --git a/build/android/src/main/res/values-v21/styles.xml b/build/android/src/main/res/values-v21/styles.xml index 52ab00845..45b00e2b2 100644 --- a/build/android/src/main/res/values-v21/styles.xml +++ b/build/android/src/main/res/values-v21/styles.xml @@ -19,4 +19,6 @@ + +