New progressbar when delete/copy, NPE and others fixes (#84)

Realize Indeterminate spinner when delete/copy, fix NPE and WindowBadToken
This commit is contained in:
berkut 2017-07-21 22:07:41 +03:00 committed by Maksim Gamarnik
parent 57cc1e4560
commit 3bd0a32b11
9 changed files with 101 additions and 38 deletions

View File

@ -2,12 +2,14 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mobi.MultiCraft"
android:installLocation="auto"
android:versionCode="52"
android:versionCode="56"
android:versionName="@string/ver">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<application
android:allowBackup="true"
@ -47,7 +49,16 @@
android:name="mobi.MultiCraft.UnzipService"
android:enabled="true"
android:exported="false" />
<service android:name="mobi.MultiCraft.FireIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name="mobi.MultiCraft.FireMsgService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>

View File

@ -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, Void, String> {
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) {

View File

@ -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);
}

View File

@ -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<Activity> 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);
}
});
if (mainActivityRef.get() != null && !mainActivityRef.get().isFinishing()) {
dialog.show();
} else {
if (sCallback != null) {
sCallback.onNegative();
}
}
}
private static void clearSharedPreferences(Context context) {

View File

@ -16,13 +16,24 @@
android:max="100"
android:visibility="gone" />
<ProgressBar
android:id="@+id/PB2"
style="@style/IndeterminateProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="90dp"
android:layout_marginRight="90dp"
android:indeterminate="true"
android:visibility="gone" />
<TextView
android:id="@+id/tv_progress_circle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="@id/PB2"
android:background="@android:color/transparent"
android:gravity="center"
android:text="@string/loading"
android:textColor="#FEFEFE"
android:visibility="gone" />

View File

@ -6,7 +6,6 @@
<string name="loading">Загрузка&#8230;</string>
<string name="notification_title">Загрузка MultiCraft</string>
<string name="notification_description">Осталось меньше минуты&#8230;</string>
<!-- разрешения -->
<string name="explain">Для корректной работы, игре требуется разрешение записывать в память устройтсва.</string>
<string name="location">Знание местоположения обеспечивает Вам лучшее взаимодействие с игрой</string>
@ -21,6 +20,7 @@
<string name="ignore">Игнорировать</string>
<string name="later">Позже</string>
<string name="update">Обновить</string>
<!-- reminder -->
<string name="message">Сообщение</string>
</resources>

View File

@ -19,4 +19,6 @@
</style>
<style name="CustomLollipopDialogStyle" parent="android:Theme.Material.Light.Dialog.Alert" />
<style name="IndeterminateProgressBar" parent="android:Widget.Material.ProgressBar.Large"/>
</resources>

View File

@ -2,8 +2,7 @@
<resources>
<string name="app_name" translatable="false">MultiCraft</string>
<string name="ver" translatable="false">1.1.7</string>
<string name="ver" translatable="false">1.1.8</string>
<!-- preparation for start -->
<string name="rm_old">Preparing to update&#8230;</string>
@ -14,7 +13,7 @@
<!-- permission block -->
<string name="explain">Game need permission to write files to storage memory.</string>
<string name="location">Location permission provide you better interaction with game</string>
<!-- rate dialog -->
<!-- rate input_dialog -->
<string name="rta_dialog_title">Please, rate MultiCraft!</string>
<string name="rate_title">Title</string>
<string name="rate_submit">SUBMIT</string>
@ -22,7 +21,7 @@
<string name="sad">We are sorry that you did not like the game!</string>
<string name="recommend">Loading recommendations \n for you \n %1$s</string>
<!--update dialog -->
<!--update input_dialog -->
<string name="update">Update</string>
<string name="later">Later</string>
<string name="ignore">Ignore</string>

View File

@ -24,4 +24,7 @@
<item name="android:minHeight">10dip</item>
<item name="android:maxHeight">20dip</item>
</style>
<style name="IndeterminateProgressBar" parent="android:Widget.Holo.ProgressBar.Large"/>
</resources>