Last fixes

This commit is contained in:
stom79 2018-01-06 17:13:18 +01:00
parent 071ba32d51
commit 33aaee824f
6 changed files with 156 additions and 97 deletions

View File

@ -14,6 +14,7 @@
* see <http://www.gnu.org/licenses>. */ * see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.activities; package fr.gouv.etalab.mastodon.activities;
import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.ActivityManager; import android.app.ActivityManager;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
@ -22,6 +23,7 @@ import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.graphics.PorterDuff; import android.graphics.PorterDuff;
import android.net.Uri; import android.net.Uri;
@ -32,6 +34,7 @@ import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout; import android.support.design.widget.AppBarLayout;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout; import android.support.design.widget.TabLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
@ -94,6 +97,7 @@ import fr.gouv.etalab.mastodon.interfaces.OnRetrieveInstanceInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMetaDataInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMetaDataInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveRemoteAccountInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveRemoteAccountInterface;
import fr.gouv.etalab.mastodon.interfaces.OnUpdateAccountInfoInterface; import fr.gouv.etalab.mastodon.interfaces.OnUpdateAccountInfoInterface;
import fr.gouv.etalab.mastodon.services.BackupStatusService;
import fr.gouv.etalab.mastodon.services.LiveNotificationService; import fr.gouv.etalab.mastodon.services.LiveNotificationService;
import fr.gouv.etalab.mastodon.sqlite.Sqlite; import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsAsyncTask;
@ -105,6 +109,7 @@ import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import static fr.gouv.etalab.mastodon.helper.Helper.ADD_USER_INTENT; import static fr.gouv.etalab.mastodon.helper.Helper.ADD_USER_INTENT;
import static fr.gouv.etalab.mastodon.helper.Helper.CHANGE_THEME_INTENT; import static fr.gouv.etalab.mastodon.helper.Helper.CHANGE_THEME_INTENT;
import static fr.gouv.etalab.mastodon.helper.Helper.CHANGE_USER_INTENT; import static fr.gouv.etalab.mastodon.helper.Helper.CHANGE_USER_INTENT;
import static fr.gouv.etalab.mastodon.helper.Helper.EXTERNAL_STORAGE_REQUEST_CODE;
import static fr.gouv.etalab.mastodon.helper.Helper.HOME_TIMELINE_INTENT; import static fr.gouv.etalab.mastodon.helper.Helper.HOME_TIMELINE_INTENT;
import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION; import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION;
import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_TARGETED_ACCOUNT; import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_TARGETED_ACCOUNT;
@ -735,6 +740,21 @@ public abstract class BaseMainActivity extends BaseActivity
.setIcon(android.R.drawable.ic_dialog_alert) .setIcon(android.R.drawable.ic_dialog_alert)
.show(); .show();
return true; return true;
case R.id.action_export:
if(Build.VERSION.SDK_INT >= 23 ){
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(BaseMainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_REQUEST_CODE);
} else {
Intent backupIntent = new Intent(BaseMainActivity.this, BackupStatusService.class);
backupIntent.putExtra("userId", userId);
startService(backupIntent);
}
}else{
Intent backupIntent = new Intent(BaseMainActivity.this, BackupStatusService.class);
backupIntent.putExtra("userId", userId);
startService(backupIntent);
}
return true;
default: default:
return true; return true;
} }

View File

@ -350,8 +350,10 @@ public class API {
apiResponse.setMax_id(httpsConnection.getMax_id()); apiResponse.setMax_id(httpsConnection.getMax_id());
} catch (HttpsConnection.HttpsConnectionException e) { } catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e); setError(e.getStatusCode(), e);
e.printStackTrace();
}catch (Exception e) { }catch (Exception e) {
setDefaultError(e); setDefaultError(e);
e.printStackTrace();
} }
apiResponse.setStatuses(statuses); apiResponse.setStatuses(statuses);
return apiResponse; return apiResponse;

View File

@ -461,6 +461,24 @@ public class Helper {
return dateFormat.format(date); return dateFormat.format(date);
} }
/**
* Convert a date in String -> format yyyy-MM-dd HH:mm:ss
* @param context Context
* @param date Date
* @return String
*/
public static String dateFileToString(Context context, Date date) {
Locale userLocale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
userLocale = context.getResources().getConfiguration().getLocales().get(0);
} else {
//noinspection deprecation
userLocale = context.getResources().getConfiguration().locale;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss",userLocale);
return dateFormat.format(date);
}
/** /**
* Convert String date from db to Date Object * Convert String date from db to Date Object
* @param stringDate date to convert * @param stringDate date to convert

View File

@ -22,15 +22,17 @@ import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Html; import android.text.Html;
import org.json.JSONObject; import android.widget.Toast;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileOutputStream;
import java.io.IOException; import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -56,6 +58,7 @@ import static fr.gouv.etalab.mastodon.helper.Helper.notify_user;
public class BackupStatusService extends IntentService { public class BackupStatusService extends IntentService {
private static int instanceRunning = 0;
/** /**
* Creates an IntentService. Invoked by your subclass's constructor. * Creates an IntentService. Invoked by your subclass's constructor.
* *
@ -79,115 +82,124 @@ public class BackupStatusService extends IntentService {
@Override @Override
protected void onHandleIntent(@Nullable Intent intent) { protected void onHandleIntent(@Nullable Intent intent) {
if( instanceRunning == 0 ){
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), R.string.data_export_start, Toast.LENGTH_LONG).show();
}
});
}else {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), R.string.data_export_running, Toast.LENGTH_LONG).show();
}
});
return;
}
instanceRunning++;
String message;
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
if( userId == null)
return;
SQLiteDatabase db = Sqlite.getInstance(BackupStatusService.this, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); SQLiteDatabase db = Sqlite.getInstance(BackupStatusService.this, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account account = new AccountDAO(getApplicationContext(), db).getAccountByID(userId); Account account = new AccountDAO(getApplicationContext(), db).getAccountByID(userId);
API api = new API(getApplicationContext(), account.getId(), account.getToken()); API api = new API(getApplicationContext(), account.getInstance(), account.getToken());
String max_id = "0";
int statusToBackUp = account.getStatuses_count();
List<Status> backupStatus = new ArrayList<>();
while (max_id != null){
APIResponse apiResponse = api.getStatus(userId, null);
max_id = apiResponse.getMax_id();
List<Status> statuses = apiResponse.getStatuses();
if (statuses.size() > 0)
backupStatus.addAll(statuses);
}
String message;
String fileName = account.getAcct()+"@"+account.getInstance()+ Helper.dateToString(getApplicationContext(), new Date())+".csv";
String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
String fullPath = filePath+"/"+fileName;
try { try {
FileWriter fw = new FileWriter(fullPath); String fullPath;
fw.append("id"); Intent intentOpen;
fw.append(','); String max_id = null;
fw.append("uri"); int statusToBackUp = account.getStatuses_count();
fw.append(','); List<Status> backupStatus = new ArrayList<>();
fw.append("url"); do {
fw.append(','); APIResponse apiResponse = api.getStatus(userId, max_id);
fw.append("account"); max_id = apiResponse.getMax_id();
fw.append(','); List<Status> statuses = apiResponse.getStatuses();
fw.append("in_reply_to_id"); if (statuses.size() > 0)
fw.append(','); backupStatus.addAll(statuses);
fw.append("in_reply_to_account_id"); }while (max_id != null);
fw.append(',');
fw.append("content"); String fileName = account.getAcct()+"@"+account.getInstance()+ Helper.dateFileToString(getApplicationContext(), new Date())+".csv";
fw.append(','); String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
fw.append("created_at"); fullPath = filePath+"/"+fileName;
fw.append(','); PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(fullPath)), "UTF-8"));
fw.append("reblogs_count"); StringBuilder builder = new StringBuilder();
fw.append(','); builder.append("id").append(',');
fw.append("favourites_count"); builder.append("uri").append(',');
fw.append(','); builder.append("url").append(',');
fw.append("sensitive"); builder.append("account").append(',');
fw.append(','); builder.append("in_reply_to_id").append(',');
fw.append("spoiler_text"); builder.append("in_reply_to_account_id").append(',');
fw.append(','); builder.append("content").append(',');
fw.append("visibility"); builder.append("created_at").append(',');
fw.append(','); builder.append("reblogs_count").append(',');
fw.append("media_attachments"); builder.append("favourites_count").append(',');
fw.append('\n'); builder.append("sensitive").append(',');
builder.append("spoiler_text").append(',');
builder.append("visibility").append(',');
builder.append("media_attachments");
builder.append('\n');
for( Status status: backupStatus){ for( Status status: backupStatus){
fw.append(status.getId()); //excludes reblog
fw.append(','); if( status.getReblog() != null){
fw.append(status.getUri()); statusToBackUp = statusToBackUp - 1;
fw.append(','); continue;
fw.append(status.getUrl()); }
fw.append(','); builder.append("\"").append(status.getId()).append("\"").append(',');
fw.append(status.getAccount().getAcct()); builder.append("\"").append(status.getUri()).append("\"").append(',');
fw.append(','); builder.append("\"").append(status.getUrl()).append("\"").append(',');
fw.append(status.getIn_reply_to_id()); builder.append("\"").append(status.getAccount().getAcct()).append("\"").append(',');
fw.append(','); builder.append("\"").append(status.getIn_reply_to_id()).append("\"").append(',');
fw.append(status.getIn_reply_to_account_id()); builder.append("\"").append(status.getIn_reply_to_account_id()).append("\"").append(',');
fw.append(',');
String content; String content;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
content = Html.fromHtml(status.getContentTranslated(), Html.FROM_HTML_MODE_LEGACY).toString(); content = Html.fromHtml(status.getContent(), Html.FROM_HTML_MODE_LEGACY).toString();
else else
//noinspection deprecation //noinspection deprecation
content = Html.fromHtml(status.getContentTranslated()).toString(); content = Html.fromHtml(status.getContent()).toString();
fw.append(content); builder.append("\"").append(content.replace("\"", "'").replace("\n"," ")).append("\"").append(',');
fw.append(','); builder.append("\"").append(Helper.shortDateTime(getApplicationContext(), status.getCreated_at())).append("\"").append(',');
fw.append(Helper.shortDateTime(getApplicationContext(), status.getCreated_at())); builder.append("\"").append(String.valueOf(status.getReblogs_count())).append("\"").append(',');
fw.append(','); builder.append("\"").append(String.valueOf(status.getFavourites_count())).append("\"").append(',');
fw.append(String.valueOf(status.getReblogs_count())); builder.append("\"").append(String.valueOf(status.isSensitive())).append("\"").append(',');
fw.append(','); builder.append("\"").append(status.getSpoiler_text() !=null?status.getSpoiler_text():"").append("\"").append(',');
fw.append(String.valueOf(status.getFavourites_count())); builder.append("\"").append(status.getVisibility()).append("\"").append(',');
fw.append(',');
fw.append(String.valueOf(status.isSensitive()));
fw.append(',');
fw.append(status.getSpoiler_text() !=null?status.getSpoiler_text():"");
fw.append(',');
fw.append(status.getVisibility());
fw.append(',');
if( status.getMedia_attachments() != null && status.getMedia_attachments().size() > 0){ if( status.getMedia_attachments() != null && status.getMedia_attachments().size() > 0){
builder.append("\"");
for(Attachment attachment: status.getMedia_attachments()){ for(Attachment attachment: status.getMedia_attachments()){
fw.append(attachment.getRemote_url()).append("\n"); builder.append(attachment.getUrl()).append(" ");
} }
builder.append("\"");
}else { }else {
fw.append(""); builder.append("\"\"");
} }
fw.append('\n'); builder.append('\n');
} }
fw.flush(); pw.write(builder.toString());
fw.close(); pw.close();
message = getString(R.string.data_export_success, account.getAcct()); message = getString(R.string.data_export_success, String.valueOf(statusToBackUp), String.valueOf(backupStatus.size()));
} catch (IOException e) { intentOpen = new Intent();
intentOpen.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://" + fullPath);
intentOpen.setDataAndType(uri, "text/csv");
long notif_id = Long.parseLong(account.getId());
int notificationId = ((notif_id + 3) > 2147483647) ? (int) (2147483647 - notif_id - 3) : (int) (notif_id + 3);
String title = getString(R.string.data_export_toots, account.getAcct());
notify_user(getApplicationContext(), intentOpen, notificationId, BitmapFactory.decodeResource(getResources(),
R.drawable.mastodonlogo), title, message);
} catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
message = getString(R.string.data_export_error, account.getAcct()); message = getString(R.string.data_export_error, account.getAcct());
final String finalMessage = message;
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), finalMessage, Toast.LENGTH_LONG).show();
}
});
} }
long notif_id = Long.parseLong(account.getId()); instanceRunning--;
int notificationId = ((notif_id + 3) > 2147483647) ? (int) (2147483647 - notif_id - 3) : (int) (notif_id + 3);
Intent intentOpen = new Intent();
intentOpen.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://" + fullPath);
intentOpen.setDataAndType(uri, "text/csv");
notify_user(getApplicationContext(), intentOpen, notificationId, BitmapFactory.decodeResource(getResources(),
R.drawable.mastodonlogo), getString(R.string.data_export), message);
} }

View File

@ -21,6 +21,10 @@
android:id="@+id/action_cache" android:id="@+id/action_cache"
android:title="@string/action_cache" android:title="@string/action_cache"
app:showAsAction="never" /> app:showAsAction="never" />
<item
android:id="@+id/action_export"
android:title="@string/data_export"
app:showAsAction="never" />
<item <item
android:id="@+id/action_logout" android:id="@+id/action_logout"
android:title="@string/action_logout" android:title="@string/action_logout"

View File

@ -468,7 +468,10 @@
<string name="media_ready">Media has been loaded. Click here to display it.</string> <string name="media_ready">Media has been loaded. Click here to display it.</string>
<string name="data_export">Data export</string> <string name="data_export_start">This action can be quite long. You will be notified when it will be finished.</string>
<string name="data_export_success">Data have been exported for %1$s</string> <string name="data_export_running">Still running, please wait…</string>
<string name="data_export">Export statuses</string>
<string name="data_export_toots">Export statuses for %1$s</string>
<string name="data_export_success">%1$s toots out of %2$s have been exported.</string>
<string name="data_export_error">Something went wrong when exporting data for %1$s</string> <string name="data_export_error">Something went wrong when exporting data for %1$s</string>
</resources> </resources>