Skip to content

Commit

Permalink
Backup & restore
Browse files Browse the repository at this point in the history
Tile entity icons
Code clean up
  • Loading branch information
meow committed Feb 3, 2020
1 parent 0104b26 commit 72c4e71
Show file tree
Hide file tree
Showing 66 changed files with 1,207 additions and 577 deletions.
18 changes: 13 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ android {
applicationId 'rbq2012.blocktopograph'
minSdkVersion 16
targetSdkVersion 29
versionCode 1090001
versionName "1.9.1"
versionCode 1090003
versionName "1.9.2"
vectorDrawables.useSupportLibrary = true
}
dataBinding {
enabled true
}
configurations {
implementation.exclude group: 'org.jetbrains', module: 'annotations'
}
buildTypes {

debug {
minifyEnabled false
multiDexEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
ext.alwaysUpdateBuildId = false
}
Expand All @@ -43,20 +47,24 @@ dependencies {
implementation 'com.github.woxthebox:draglistview:1.6.3'
implementation 'com.andreabaccega:android-edittext-validator:1.3.5'
//core is the new recommended alias for analytics
implementation 'com.google.firebase:firebase-core:17.2.1'
implementation 'com.google.firebase:firebase-core:17.2.2'
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
implementation 'org.jetbrains:annotations-java5:15.0'
//implementation 'org.jetbrains:annotations-java5:15.0'
implementation 'com.tomergoldst.android:tooltips:1.0.10'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.google.android.material:material:1.2.0-alpha03'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
//implementation 'com.github.MikeOrtiz:TouchImageView:2.1.1'
implementation 'com.github.chrisbanes:PhotoView:2.3.0'
implementation 'com.github.florent37:expansionpanel:1.2.2'
implementation 'com.jbvincey:nestedradiobutton:1.0'
implementation 'net.lingala.zip4j:zip4j:2.3.1'
implementation 'commons-io:commons-io:2.4'
implementation 'org.apache.commons:commons-lang3:3.9'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.1'
}

apply plugin: 'com.google.gms.google-services'
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@
android:name=".UnsatisfiedLinkErrorActivity"
android:label="@string/some_thing_went_wrong" />

<activity android:name=".test.VisualizationActivity" />
<activity
android:name=".BackupActivity"
android:label="@string/backup_amp_restore"
android:theme="@style/AppTheme.Dialog" />

<meta-data
android:name="android.support.VERSION"
android:value="28.0.0"
Expand Down
209 changes: 209 additions & 0 deletions app/src/main/java/com/mithrilmania/blocktopograph/BackupActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package com.mithrilmania.blocktopograph;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mithrilmania.blocktopograph.backup.Backup;
import com.mithrilmania.blocktopograph.backup.WorldBackups;
import com.mithrilmania.blocktopograph.databinding.ActivityBackupBinding;
import com.mithrilmania.blocktopograph.databinding.ItemBackupBinding;
import com.mithrilmania.blocktopograph.util.Consumer;
import com.mithrilmania.blocktopograph.util.UiUtil;

import java.util.Date;

public class BackupActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

private ActivityBackupBinding mBinding;

private WorldBackups mBackups;

private MeowAdapter mBackupsListAdapter;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_backup);
World world = (World) (savedInstanceState == null
? getIntent().getSerializableExtra(World.ARG_WORLD_SERIALIZED)
: savedInstanceState.getSerializable(World.ARG_WORLD_SERIALIZED));
if (world == null) {
finish();
return;
}
mBackups = new WorldBackups(world);
mBackups.loadConfig();
mBinding.setBackups(mBackups);
mBinding.cbAutoBackup.setChecked(mBackups.autoBackup);
mBinding.cbAutoDelete.setChecked(mBackups.autoDelete);
mBinding.cbAutoBackup.setOnCheckedChangeListener(this);
mBinding.cbAutoDelete.setOnCheckedChangeListener(this);
mBinding.list.setLayoutManager(new LinearLayoutManager(this));
mBackupsListAdapter = new MeowAdapter(this::onClickRestore, this::onClickDelete);
mBinding.list.setAdapter(mBackupsListAdapter);
resetList();
}

@SuppressLint("StaticFieldLeak")
public void onClickBackup(View view) {
// The progress won't take long then it won't leak.
String name = mBinding.editName.getText().toString().trim();
String bakName = name.equals("") ? "Backup" : name;
AlertDialog dia = UiUtil.buildProgressWaitDialog(this, R.string.general_please_wait, null);
dia.show();
new AsyncTask<WorldBackups, Void, Boolean>() {
@Override
protected Boolean doInBackground(WorldBackups... worldBackups) {
return worldBackups[0].createNewBackup(bakName, new Date());
}

@Override
protected void onPostExecute(Boolean aBoolean) {
if (aBoolean)
UiUtil.toast(BackupActivity.this, R.string.general_done);
else
UiUtil.toast(BackupActivity.this, R.string.general_failed);
synchronized (dia) {
if (dia.isShowing()) dia.dismiss();
}
resetList();
}
}.execute(mBackups);
}

@SuppressLint("StaticFieldLeak")
private void onClickRestore(@NonNull Backup backup) {
// The progress won't take long then it won't leak.
new AlertDialog.Builder(this).setTitle(R.string.restore_caption)
.setMessage(R.string.restore_warn)
.setPositiveButton(android.R.string.ok, (use, less) -> {
AlertDialog dia = UiUtil.buildProgressWaitDialog(this, R.string.general_please_wait, null);
dia.show();
new AsyncTask<Backup, Void, Boolean>() {
@Override
protected Boolean doInBackground(Backup... backups) {
return mBackups.restoreBackup(backups[0]);
}

@Override
protected void onPostExecute(Boolean aBoolean) {
if (aBoolean)
UiUtil.toast(BackupActivity.this, R.string.general_done);
else
UiUtil.toast(BackupActivity.this, R.string.general_failed);
synchronized (dia) {
if (dia.isShowing()) dia.dismiss();
}
}
}.execute(backup);
}).setNegativeButton(android.R.string.cancel, null).create().show();
}

private void onClickDelete(@NonNull Backup backup) {
new AlertDialog.Builder(this).setTitle(R.string.backup_delete_caption)
.setMessage(R.string.backup_delete_warn)
.setPositiveButton(android.R.string.ok, (use, less) -> {
mBackups.deleteBackup(backup);
resetList();
})
.setNegativeButton(android.R.string.cancel, null).create().show();
}

private void resetList() {
mBackups.cleanOldBackups(new Date());
mBackupsListAdapter.setData(mBackups.getBackups());
}


@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
switch (compoundButton.getId()) {
case R.id.cb_auto_backup:
if (b) {
new AlertDialog.Builder(this)
.setMessage(R.string.enable_auto_backup_note)
.setTitle(R.string.enable_auto_backup_title)
.setPositiveButton(android.R.string.ok, (use, less) -> mBackups.setAutoBackup(true, getString(R.string.backup_readme)))
.create().show();
} else
mBackups.setAutoBackup(false, getString(R.string.backup_readme));
break;
case R.id.cb_auto_delete:
mBackups.setAutoDelete(b, getString(R.string.backup_readme));
break;
}
}

private static class MeowAdapter extends RecyclerView.Adapter<MeowAdapter.MeowHolder> {

@NonNull
private Consumer<Backup> mRestorer;

@NonNull
private Consumer<Backup> mDeleter;

@Nullable
private Backup[] data;

MeowAdapter(@NonNull Consumer<Backup> restorer,
@NonNull Consumer<Backup> deleter) {
mRestorer = restorer;
mDeleter = deleter;
}

public void setData(@Nullable Backup[] data) {
this.data = data;
notifyDataSetChanged();
}

@NonNull
@Override
public MeowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new MeowHolder(DataBindingUtil.inflate(
LayoutInflater.from(parent.getContext()), R.layout.item_backup, parent, false
));
}

@Override
public void onBindViewHolder(@NonNull MeowHolder holder, int position) {
if (data != null) holder.setData(data[position]);
}

@Override
public int getItemCount() {
return data == null ? 0 : data.length;
}

private class MeowHolder extends RecyclerView.ViewHolder {

@NonNull
private ItemBackupBinding mBinding;

MeowHolder(@NonNull ItemBackupBinding binding) {
super(binding.getRoot());
mBinding = binding;
mBinding.buttonRestore.setOnClickListener(v -> mRestorer.accept(mBinding.getBackup()));
mBinding.buttonDelete.setOnClickListener(v -> mDeleter.accept(mBinding.getBackup()));
}

public void setData(Backup backup) {
mBinding.setBackup(backup);
}

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
import com.tomergoldst.tooltips.ToolTip;
import com.tomergoldst.tooltips.ToolTipsManager;

import org.jetbrains.annotations.NotNull;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -95,7 +93,7 @@ public void onClickChangeBiome(View view) {
startActivityForResult(new Intent(this, BiomeSelectDialog.class), REQUEST_CODE_PICK_BIOME);
}

private void setBiomeToView(@NotNull Biome biome) {
private void setBiomeToView(@NonNull Biome biome) {
UiUtil.blendBlockColor(mBinding.biomeView.root, biome);
mBinding.biomeView.setBiome(biome);
}
Expand Down
9 changes: 4 additions & 5 deletions app/src/main/java/com/mithrilmania/blocktopograph/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;

import com.crashlytics.android.Crashlytics;
import com.google.firebase.analytics.FirebaseAnalytics;

import org.jetbrains.annotations.NotNull;

import java.io.PrintWriter;
import java.io.StringWriter;

import androidx.annotation.NonNull;
import io.fabric.sdk.android.Fabric;

public class Log {
Expand All @@ -36,12 +35,12 @@ private static String concat(@NonNull Object caller, @NonNull String msg) {
return clazz.getSimpleName() + ": " + msg;
}

public static void enableFirebaseAnalytics(@NotNull Context context) {
public static void enableFirebaseAnalytics(@NonNull Context context) {
getFirebaseAnalytics(context).setAnalyticsCollectionEnabled(true);
mIsFirebaseAnalyticsEnabled = true;
}

public static void enableCrashlytics(@NotNull Context context) {
public static void enableCrashlytics(@NonNull Context context) {
if (!BuildConfig.DEBUG) {
Fabric.with(context, new Crashlytics());
mIsCrashlyticsEnabled = true;
Expand Down
19 changes: 9 additions & 10 deletions app/src/main/java/com/mithrilmania/blocktopograph/World.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.mithrilmania.blocktopograph;

import android.content.Context;
import android.os.Bundle;
import android.util.SparseIntArray;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.litl.leveldb.Iterator;
import com.mithrilmania.blocktopograph.chunk.Chunk;
import com.mithrilmania.blocktopograph.map.Dimension;
Expand All @@ -24,9 +28,6 @@
import java.io.Serializable;
import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class World implements Serializable {

// The World (just a WorldData handle) is serializable, this is the tag used in the android workflow
Expand Down Expand Up @@ -55,7 +56,7 @@ public class World implements Serializable {

private boolean mHaveBackgroundJob;

public World(File worldFolder, String mark) throws WorldLoadException {
public World(File worldFolder, String mark, @NonNull Context context) throws WorldLoadException {

if (!worldFolder.exists())
throw new WorldLoadException("Error: '" + worldFolder.getPath() + "' does not exist!");
Expand All @@ -66,15 +67,14 @@ public World(File worldFolder, String mark) throws WorldLoadException {

// check for a custom world name
File levelNameTxt = new File(this.worldFolder, "levelname.txt");
this.levelFile = new File(this.worldFolder, "level.dat");
if (levelNameTxt.exists())
worldName = IoUtil.readTextFileFirstLine(levelNameTxt);// new way of naming worlds
else worldName = this.worldFolder.getName();// legacy way of naming worlds
else if (levelFile.exists())
worldName = this.worldFolder.getName();// legacy way of naming worlds
else worldName = context.getString(R.string.world_name_broken);


this.levelFile = new File(this.worldFolder, "level.dat");
if (!levelFile.exists())
throw new WorldLoadException("Error: Level-file: '" + levelFile.getPath() + "' does not exist!");

}

public CompoundTag getLevel() {
Expand All @@ -91,7 +91,6 @@ public String getWorldDisplayName() {
if (worldName == null) return null;
//return worldname, without special color codes
// (character prefixed by the section-sign character)
// Short quick regex, shouldn't affect performance too much
return worldName.replaceAll("\u00A7.", "");
}

Expand Down
Loading

0 comments on commit 72c4e71

Please sign in to comment.