Fix import/export feature

This commit is contained in:
tom79 2019-03-02 15:43:40 +01:00
parent 90763435a3
commit 8cde45a301
7 changed files with 102 additions and 79 deletions

View File

@ -93,7 +93,6 @@ dependencies {
implementation 'org.apache.poi:poi:3.16'
implementation 'com.github.mabbas007:TagsEditText:1.0.5'
implementation 'com.jaredrummler:material-spinner:1.3.1'
implementation 'com.github.stom79:SQLite2XL:1.2'
implementation "com.tonyodev.fetch2:fetch2:2.3.6"
playstoreImplementation "io.github.kobakei:ratethisapp:$ratethisappLibraryVersion"
}

View File

@ -112,6 +112,15 @@
android:pathPrefix="/@"
/>
</intent-filter>
<intent-filter
>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:host="*" />
<data android:pathPattern=".*\\.fedilab" />
</intent-filter>
</activity>
<activity android:name=".activities.LoginActivity"
android:windowSoftInputMode="stateAlwaysHidden"

View File

@ -78,8 +78,6 @@ import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import com.ajts.androidmads.library.ExcelToSQLite;
import com.ajts.androidmads.library.SQLiteToExcel;
import org.json.JSONArray;
import org.json.JSONException;
@ -154,6 +152,7 @@ import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.InstancesDAO;
import fr.gouv.etalab.mastodon.sqlite.SearchDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.sqlite.StatusCacheDAO;
import static fr.gouv.etalab.mastodon.asynctasks.ManageFiltersAsyncTask.action.GET_ALL_FILTER;
import static fr.gouv.etalab.mastodon.helper.Helper.ADD_USER_INTENT;
@ -178,6 +177,8 @@ import static fr.gouv.etalab.mastodon.helper.Helper.menuAccounts;
import static fr.gouv.etalab.mastodon.helper.Helper.unCheckAllMenuItems;
import static fr.gouv.etalab.mastodon.helper.Helper.updateHeaderAccountInfo;
import static fr.gouv.etalab.mastodon.sqlite.Sqlite.DB_NAME;
import static fr.gouv.etalab.mastodon.sqlite.Sqlite.exportDB;
import static fr.gouv.etalab.mastodon.sqlite.Sqlite.importDB;
public abstract class BaseMainActivity extends BaseActivity
@ -1407,7 +1408,6 @@ public abstract class BaseMainActivity extends BaseActivity
return true;
case R.id.action_import_data:
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
if (ContextCompat.checkSelfPermission(BaseMainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED) {
@ -1420,12 +1420,12 @@ public abstract class BaseMainActivity extends BaseActivity
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
intent.setType("application/vnd.ms-excel");
String[] mimetypes = {"application/vnd.ms-excel"};
intent.setType("*/*");
String[] mimetypes = {"*/*"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(intent, PICK_IMPORT);
}else {
intent.setType("application/vnd.ms-excel");
intent.setType("*/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent chooserIntent = Intent.createChooser(intent, getString(R.string.toot_select_import));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});
@ -1439,40 +1439,7 @@ public abstract class BaseMainActivity extends BaseActivity
return true;
}
}
SQLiteToExcel sqliteToExcel = new SQLiteToExcel(BaseMainActivity.this, DB_NAME);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
final String fileName = "Mastalab_export_"+timeStamp+".xls";
List<String> excludedValues = new ArrayList<>();
excludedValues.add(Sqlite.TABLE_TRACKING_BLOCK);
excludedValues.add(Sqlite.TABLE_STATUSES_CACHE);
excludedValues.add(Sqlite.TABLE_CUSTOM_EMOJI);
excludedValues.add(Sqlite.TABLE_BOOST_SCHEDULE);
sqliteToExcel.setExcludeValuesFromTables(excludedValues);
sqliteToExcel.exportAllTables(fileName, new SQLiteToExcel.ExportListener() {
@Override
public void onStart() {
}
@Override
public void onCompleted(String filePath) {
final Intent intent = new Intent();
Random r = new Random();
final int notificationIdTmp = r.nextInt(10000);
File file = new File(filePath);
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/vnd.ms-excel");
Helper.notify_user(getApplicationContext(), intent, notificationIdTmp, BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher), Helper.NotifType.STORE, getString(R.string.save_over), getString(R.string.download_from, fileName));
Toasty.success(getApplicationContext(), getString(R.string.data_base_exported),Toast.LENGTH_LONG).show();
}
@Override
public void onError(Exception e) {
e.printStackTrace();
Toasty.error(getApplicationContext(), getString(R.string.data_export_error_simple),Toast.LENGTH_LONG).show();
}
});
exportDB(BaseMainActivity.this);
return true;
default:
return true;
@ -2348,26 +2315,11 @@ public abstract class BaseMainActivity extends BaseActivity
Toasty.error(getApplicationContext(),getString(R.string.toot_select_file_error),Toast.LENGTH_LONG).show();
return;
}
ExcelToSQLite excelToSQLite = new ExcelToSQLite(getApplicationContext(), DB_NAME, true);
String filename = Helper.getFilePathFromURI(getApplicationContext(), data.getData());
assert filename != null;
excelToSQLite.importFromFile(filename, new ExcelToSQLite.ImportListener() {
@Override
public void onStart() {
Toasty.success(getApplicationContext(),getString(R.string.data_import_start),Toast.LENGTH_LONG).show();
}
importDB(BaseMainActivity.this, filename);
@Override
public void onCompleted(String dbName) {
Toasty.success(getApplicationContext(),getString(R.string.data_import_success_simple),Toast.LENGTH_LONG).show();
Helper.logoutCurrentUser(BaseMainActivity.this);
}
@Override
public void onError(Exception e) {
Toasty.error(getApplicationContext(),getString(R.string.data_import_error_simple),Toast.LENGTH_LONG).show();
}
});
}else{
Toasty.error(getApplicationContext(),getString(R.string.toot_select_file_error),Toast.LENGTH_LONG).show();
}
}

View File

@ -55,7 +55,6 @@ import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.ajts.androidmads.library.ExcelToSQLite;
import com.elconfidencial.bubbleshowcase.BubbleShowCase;
import com.elconfidencial.bubbleshowcase.BubbleShowCaseBuilder;
import com.elconfidencial.bubbleshowcase.BubbleShowCaseListener;
@ -89,6 +88,7 @@ import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor;
import static fr.gouv.etalab.mastodon.helper.Helper.changeMaterialSpinnerColor;
import static fr.gouv.etalab.mastodon.helper.Helper.convertDpToPixel;
import static fr.gouv.etalab.mastodon.sqlite.Sqlite.DB_NAME;
import static fr.gouv.etalab.mastodon.sqlite.Sqlite.importDB;
/**
@ -821,26 +821,10 @@ public class LoginActivity extends BaseActivity {
Toasty.error(getApplicationContext(),getString(R.string.toot_select_file_error),Toast.LENGTH_LONG).show();
return;
}
ExcelToSQLite excelToSQLite = new ExcelToSQLite(getApplicationContext(), DB_NAME, true);
String filename = Helper.getFilePathFromURI(getApplicationContext(), data.getData());
assert filename != null;
excelToSQLite.importFromFile(filename, new ExcelToSQLite.ImportListener() {
@Override
public void onStart() {
Toasty.success(getApplicationContext(),getString(R.string.data_import_start),Toast.LENGTH_LONG).show();
}
@Override
public void onCompleted(String dbName) {
Toasty.success(getApplicationContext(),getString(R.string.data_import_success_simple),Toast.LENGTH_LONG).show();
Helper.logoutCurrentUser(LoginActivity.this);
}
@Override
public void onError(Exception e) {
Toasty.error(getApplicationContext(),getString(R.string.data_import_error_simple),Toast.LENGTH_LONG).show();
}
});
importDB(LoginActivity.this, filename);
}else{
Toasty.error(getApplicationContext(),getString(R.string.toot_select_file_error),Toast.LENGTH_LONG).show();
}
}

View File

@ -919,6 +919,8 @@ public class API {
} catch (JSONException e) {
e.printStackTrace();
}
if( apiResponse == null)
apiResponse = new APIResponse();
apiResponse.setStatuses(statuses);
return apiResponse;
}

View File

@ -134,6 +134,7 @@ import java.io.OutputStream;
import java.lang.ref.WeakReference;
import java.lang.reflect.Type;
import java.net.InetAddress;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
@ -3730,4 +3731,8 @@ public class Helper {
e.printStackTrace();
}
}
}

View File

@ -15,9 +15,29 @@
package fr.gouv.etalab.mastodon.sqlite;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;
import es.dmoral.toasty.Toasty;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.LoginActivity;
import fr.gouv.etalab.mastodon.helper.Helper;
/**
* Created by Thomas on 23/04/2017.
@ -359,4 +379,56 @@ public class Sqlite extends SQLiteOpenHelper {
db.close();
}
}
public static void importDB(Activity activity, String backupDBPath) {
try {
db.close();
File dbDest = activity.getDatabasePath(DB_NAME);
File dbSource = new File(backupDBPath);
FileChannel src = new FileInputStream(dbSource).getChannel();
FileChannel dst = new FileOutputStream(dbDest).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Helper.logoutCurrentUser(activity);
} catch (Exception e) {
e.printStackTrace();
Toasty.error(activity, activity.getString(R.string.data_import_error_simple),Toast.LENGTH_LONG).show();
}
}
public static void exportDB(Context context) {
try {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String backupDBPath = "Fedilab_export_"+timeStamp+".fedilab";
File dbSource = context.getDatabasePath(DB_NAME);
File dbDest = new File(sd,backupDBPath);
FileChannel src = new FileInputStream(dbSource).getChannel();
FileChannel dst = new FileOutputStream(dbDest).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
final Intent intent = new Intent();
Random r = new Random();
final int notificationIdTmp = r.nextInt(10000);
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(dbDest);
intent.setDataAndType(uri, "*/*");
Helper.notify_user(context, intent, notificationIdTmp, BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_launcher), Helper.NotifType.STORE, context.getString(R.string.save_over), context.getString(R.string.download_from, backupDBPath));
Toasty.success(context, context.getString(R.string.data_base_exported),Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
Toasty.error(context, context.getString(R.string.data_export_error_simple), Toast.LENGTH_LONG).show();
}
}
}