From 21d7229a1142eb30c9901b93d1632f7bd11a30ae Mon Sep 17 00:00:00 2001 From: stom79 Date: Fri, 22 Feb 2019 14:06:39 +0100 Subject: [PATCH 1/4] Fix pull to refresh for GNU --- .../RetrieveMissingFeedsAsyncTask.java | 151 ++++++++++++------ 1 file changed, 100 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMissingFeedsAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMissingFeedsAsyncTask.java index b7203a4eb..7759bdba0 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMissingFeedsAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMissingFeedsAsyncTask.java @@ -28,6 +28,7 @@ import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.client.Entities.Account; import fr.gouv.etalab.mastodon.client.Entities.Conversation; import fr.gouv.etalab.mastodon.client.Entities.TagTimeline; +import fr.gouv.etalab.mastodon.client.GNUAPI; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMissingFeedsInterface; import fr.gouv.etalab.mastodon.sqlite.SearchDAO; import fr.gouv.etalab.mastodon.sqlite.Sqlite; @@ -67,60 +68,108 @@ public class RetrieveMissingFeedsAsyncTask extends AsyncTask { protected Void doInBackground(Void... params) { if( this.contextReference.get() == null) return null; - API api = new API(this.contextReference.get()); - List tempStatus = null; - APIResponse apiResponse = null; - if( type == RetrieveFeedsAsyncTask.Type.HOME) { - apiResponse = api.getHomeTimelineSinceId(since_id); - }else if( type == RetrieveFeedsAsyncTask.Type.DIRECT) - apiResponse = api.getDirectTimelineSinceId(since_id); - else if( type == RetrieveFeedsAsyncTask.Type.CONVERSATION) - apiResponse = api.getConversationTimelineSinceId(since_id); - else if( type == RetrieveFeedsAsyncTask.Type.LOCAL) - apiResponse = api.getPublicTimelineSinceId(true, since_id); - else if( type == RetrieveFeedsAsyncTask.Type.PUBLIC) - apiResponse = api.getPublicTimelineSinceId(false, since_id); - else if (type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE) - apiResponse = api.getInstanceTimelineSinceId(remoteInstance, since_id); - else if (type == RetrieveFeedsAsyncTask.Type.TAG) { - SQLiteDatabase db = Sqlite.getInstance(contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - List tagTimelines = new SearchDAO(contextReference.get(), db).getTimelineInfo(remoteInstance); - if( tagTimelines != null && tagTimelines.size() > 0){ - TagTimeline tagTimeline = tagTimelines.get(0); - boolean isArt = tagTimeline.isART(); - if( isArt) - apiResponse = api.getCustomArtTimelineSinceId(false, remoteInstance, since_id, tagTimelines.get(0).getAny(), tagTimelines.get(0).getAll(), tagTimelines.get(0).getNone()); - else - apiResponse = api.getPublicTimelineTagSinceId(remoteInstance, false, since_id, tagTimelines.get(0).getAny(), tagTimelines.get(0).getAll(), tagTimelines.get(0).getNone()); - }else{ - apiResponse = api.getPublicTimelineTag(remoteInstance, false, since_id, tagTimelines.get(0).getAny(), tagTimelines.get(0).getAll(), tagTimelines.get(0).getNone()); - } - }else if (type == RetrieveFeedsAsyncTask.Type.ART) - apiResponse = api.getArtTimelineSinceId( false, since_id, null, null, null); - if (apiResponse != null) { - if( type != RetrieveFeedsAsyncTask.Type.CONVERSATION) - tempStatus = apiResponse.getStatuses(); - else{ - List conversations = apiResponse.getConversations(); - tempStatus = new ArrayList<>(); - if( conversations != null && conversations.size() > 0){ - for(Conversation conversation: conversations){ - fr.gouv.etalab.mastodon.client.Entities.Status status = conversation.getLast_status(); - List ppConversation = new ArrayList<>(); - for (Account account : conversation.getAccounts()) - ppConversation.add(account.getAvatar()); - status.setConversationProfilePicture(ppConversation); - status.setConversationId(conversation.getId()); - tempStatus.add(status); + if(MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.GNU && MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) { + API api = new API(this.contextReference.get()); + List tempStatus = null; + APIResponse apiResponse = null; + if (type == RetrieveFeedsAsyncTask.Type.HOME) { + apiResponse = api.getHomeTimelineSinceId(since_id); + } else if (type == RetrieveFeedsAsyncTask.Type.DIRECT) + apiResponse = api.getDirectTimelineSinceId(since_id); + else if (type == RetrieveFeedsAsyncTask.Type.CONVERSATION) + apiResponse = api.getConversationTimelineSinceId(since_id); + else if (type == RetrieveFeedsAsyncTask.Type.LOCAL) + apiResponse = api.getPublicTimelineSinceId(true, since_id); + else if (type == RetrieveFeedsAsyncTask.Type.PUBLIC) + apiResponse = api.getPublicTimelineSinceId(false, since_id); + else if (type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE) + apiResponse = api.getInstanceTimelineSinceId(remoteInstance, since_id); + else if (type == RetrieveFeedsAsyncTask.Type.TAG) { + SQLiteDatabase db = Sqlite.getInstance(contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + List tagTimelines = new SearchDAO(contextReference.get(), db).getTimelineInfo(remoteInstance); + if (tagTimelines != null && tagTimelines.size() > 0) { + TagTimeline tagTimeline = tagTimelines.get(0); + boolean isArt = tagTimeline.isART(); + if (isArt) + apiResponse = api.getCustomArtTimelineSinceId(false, remoteInstance, since_id, tagTimelines.get(0).getAny(), tagTimelines.get(0).getAll(), tagTimelines.get(0).getNone()); + else + apiResponse = api.getPublicTimelineTagSinceId(remoteInstance, false, since_id, tagTimelines.get(0).getAny(), tagTimelines.get(0).getAll(), tagTimelines.get(0).getNone()); + } else { + apiResponse = api.getPublicTimelineTag(remoteInstance, false, since_id, tagTimelines.get(0).getAny(), tagTimelines.get(0).getAll(), tagTimelines.get(0).getNone()); + } + } else if (type == RetrieveFeedsAsyncTask.Type.ART) + apiResponse = api.getArtTimelineSinceId(false, since_id, null, null, null); + if (apiResponse != null) { + if (type != RetrieveFeedsAsyncTask.Type.CONVERSATION) + tempStatus = apiResponse.getStatuses(); + else { + List conversations = apiResponse.getConversations(); + tempStatus = new ArrayList<>(); + if (conversations != null && conversations.size() > 0) { + for (Conversation conversation : conversations) { + fr.gouv.etalab.mastodon.client.Entities.Status status = conversation.getLast_status(); + List ppConversation = new ArrayList<>(); + for (Account account : conversation.getAccounts()) + ppConversation.add(account.getAvatar()); + status.setConversationProfilePicture(ppConversation); + status.setConversationId(conversation.getId()); + tempStatus.add(status); + } } } - } - if( tempStatus != null) - statuses.addAll(0, tempStatus); - } - if (type == RetrieveFeedsAsyncTask.Type.HOME && statuses.size() > 0) { - MainActivity.lastHomeId = statuses.get(0).getId(); + if (tempStatus != null) + statuses.addAll(0, tempStatus); + } + if (type == RetrieveFeedsAsyncTask.Type.HOME && statuses.size() > 0) { + MainActivity.lastHomeId = statuses.get(0).getId(); + } + }else{ + GNUAPI gnuapi = new GNUAPI(this.contextReference.get()); + List tempStatus = null; + APIResponse apiResponse = null; + if (type == RetrieveFeedsAsyncTask.Type.GNU_HOME) { + apiResponse = gnuapi.getHomeTimelineSinceId(since_id); + }else if (type == RetrieveFeedsAsyncTask.Type.GNU_LOCAL) + apiResponse = gnuapi.getPublicTimelineSinceId(true, since_id); + else if (type == RetrieveFeedsAsyncTask.Type.GNU_WHOLE) + apiResponse = gnuapi.getPublicTimelineSinceId(false, since_id); + else if (type == RetrieveFeedsAsyncTask.Type.GNU_TAG) { + SQLiteDatabase db = Sqlite.getInstance(contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + List tagTimelines = new SearchDAO(contextReference.get(), db).getTimelineInfo(remoteInstance); + if (tagTimelines != null && tagTimelines.size() > 0) { + TagTimeline tagTimeline = tagTimelines.get(0); + boolean isArt = tagTimeline.isART(); + if (isArt) + apiResponse = gnuapi.getCustomArtTimelineSinceId(false, remoteInstance, since_id, tagTimelines.get(0).getAny(), tagTimelines.get(0).getAll(), tagTimelines.get(0).getNone()); + else + apiResponse = gnuapi.getPublicTimelineTagSinceId(remoteInstance, false, since_id, tagTimelines.get(0).getAny(), tagTimelines.get(0).getAll(), tagTimelines.get(0).getNone()); + } else { + apiResponse = gnuapi.getPublicTimelineTag(remoteInstance, false, since_id, tagTimelines.get(0).getAny(), tagTimelines.get(0).getAll(), tagTimelines.get(0).getNone()); + } + } + if (apiResponse != null) { + if (type != RetrieveFeedsAsyncTask.Type.CONVERSATION) + tempStatus = apiResponse.getStatuses(); + else { + List conversations = apiResponse.getConversations(); + tempStatus = new ArrayList<>(); + if (conversations != null && conversations.size() > 0) { + for (Conversation conversation : conversations) { + fr.gouv.etalab.mastodon.client.Entities.Status status = conversation.getLast_status(); + List ppConversation = new ArrayList<>(); + for (Account account : conversation.getAccounts()) + ppConversation.add(account.getAvatar()); + status.setConversationProfilePicture(ppConversation); + status.setConversationId(conversation.getId()); + tempStatus.add(status); + } + } + } + + if (tempStatus != null) + statuses.addAll(0, tempStatus); + } } return null; } From bd51be594aa54f7291b7160401f3d68529e73a78 Mon Sep 17 00:00:00 2001 From: stom79 Date: Fri, 22 Feb 2019 14:30:04 +0100 Subject: [PATCH 2/4] Hide contacts for GNU --- .../fr/gouv/etalab/mastodon/activities/TootActivity.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java index b4a9cbf01..3136f4305 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java @@ -1593,6 +1593,11 @@ public class TootActivity extends BaseActivity implements OnPostActionInterface, if( itemStore != null) itemStore.setVisible(false); } + if( MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.GNU || MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA){ + MenuItem itemContacts = menu.findItem(R.id.action_contacts); + if( itemContacts != null) + itemContacts.setVisible(false); + } return true; } From d4c3ffed58336575ddd89e3ad1e826d97816f582 Mon Sep 17 00:00:00 2001 From: stom79 Date: Sat, 23 Feb 2019 14:16:15 +0100 Subject: [PATCH 3/4] Fix export --- app/build.gradle | 2 +- .../gouv/etalab/mastodon/activities/BaseMainActivity.java | 6 +++++- .../main/java/fr/gouv/etalab/mastodon/sqlite/Sqlite.java | 2 +- app/src/main/res/values/strings.xml | 1 + 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 8fae73974..685e69fb6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -93,7 +93,7 @@ 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.0.5' + implementation 'com.github.stom79:SQLite2XL:1.2' implementation "com.tonyodev.fetch2:fetch2:2.3.6" playstoreImplementation "io.github.kobakei:ratethisapp:$ratethisappLibraryVersion" } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java index 8806e3a19..40fe566f8 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java @@ -1437,6 +1437,9 @@ public abstract class BaseMainActivity extends BaseActivity 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 excludedValues = new ArrayList<>(); + excludedValues.add(Sqlite.TABLE_TRACKING_BLOCK); + sqliteToExcel.setExcludeValuesFromTables(excludedValues); sqliteToExcel.exportAllTables(fileName, new SQLiteToExcel.ExportListener() { @Override public void onStart() { @@ -1453,10 +1456,11 @@ public abstract class BaseMainActivity extends BaseActivity 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.toast_saved),Toast.LENGTH_LONG).show(); + 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(); } }); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/Sqlite.java b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/Sqlite.java index 2458d7fbb..84bba2cab 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/Sqlite.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/Sqlite.java @@ -65,7 +65,7 @@ public class Sqlite extends SQLiteOpenHelper { static final String TABLE_BOOST_SCHEDULE = "BOOST_SCHEDULE"; //Table for blocking tracking domains - static final String TABLE_TRACKING_BLOCK = "TRACKING_BLOCK"; + public static final String TABLE_TRACKING_BLOCK = "TRACKING_BLOCK"; static final String COL_USER_ID = "USER_ID"; static final String COL_USERNAME = "USERNAME"; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e470b77ff..d22773cad 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -897,6 +897,7 @@ http calls blocked by the application List of blocked calls Submit + The data base has been exported! From c19a4dadcc769c89f75ea18f6d5cd19b5330b2bc Mon Sep 17 00:00:00 2001 From: stom79 Date: Sat, 23 Feb 2019 14:23:31 +0100 Subject: [PATCH 4/4] Restore accounts --- .../mastodon/activities/BaseMainActivity.java | 5 +- .../mastodon/activities/LoginActivity.java | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java index 40fe566f8..b0e23a48c 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java @@ -2351,10 +2351,7 @@ public abstract class BaseMainActivity extends BaseActivity @Override public void onCompleted(String dbName) { Toasty.success(getApplicationContext(),getString(R.string.data_import_success_simple),Toast.LENGTH_LONG).show(); - Intent changeAccount = new Intent(activity, MainActivity.class); - changeAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - activity.finish(); - activity.startActivity(changeAccount); + Helper.logoutCurrentUser(BaseMainActivity.this); } @Override diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/LoginActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/LoginActivity.java index ccff500b2..d4b47fb28 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/LoginActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/LoginActivity.java @@ -14,17 +14,22 @@ * see . */ package fr.gouv.etalab.mastodon.activities; +import android.Manifest; import android.annotation.SuppressLint; +import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; import android.database.sqlite.SQLiteDatabase; import android.graphics.Color; import android.net.Uri; import android.os.AsyncTask; +import android.os.Build; import android.os.Bundle; import android.support.design.widget.TextInputLayout; +import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.ActionBar; import android.support.v7.app.AlertDialog; @@ -50,6 +55,7 @@ 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; @@ -73,6 +79,7 @@ import fr.gouv.etalab.mastodon.client.Entities.Account; import fr.gouv.etalab.mastodon.client.Entities.InstanceNodeInfo; import fr.gouv.etalab.mastodon.client.GNUAPI; import fr.gouv.etalab.mastodon.client.HttpsConnection; +import fr.gouv.etalab.mastodon.fragments.SettingsFragment; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.sqlite.AccountDAO; import fr.gouv.etalab.mastodon.sqlite.Sqlite; @@ -81,6 +88,7 @@ import static fr.gouv.etalab.mastodon.helper.Helper.THEME_LIGHT; 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; /** @@ -109,6 +117,7 @@ public class LoginActivity extends BaseActivity { private LinearLayout step_login_credential, step_instance; private TextView instance_chosen; private ImageView info_instance; + private final int PICK_IMPORT = 5557; @Override protected void onCreate(Bundle savedInstanceState) { @@ -774,11 +783,68 @@ public class LoginActivity extends BaseActivity { editor.putBoolean(Helper.SET_SECURITY_PROVIDER, item.isChecked()); editor.apply(); return false; + }else if(id == R.id.action_import_data){ + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + if (ContextCompat.checkSelfPermission(LoginActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != + PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(LoginActivity.this, + new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, + TootActivity.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); + return true; + } + } + Intent 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.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes); + startActivityForResult(intent, PICK_IMPORT); + }else { + intent.setType("application/vnd.ms-excel"); + 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}); + startActivityForResult(chooserIntent, PICK_IMPORT); + } } return super.onOptionsItemSelected(item); } + @Override + protected void onActivityResult(int requestCode, int resultCode, + Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (requestCode == PICK_IMPORT && resultCode == Activity.RESULT_OK) { + if (data == null || data.getData() == null) { + 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 = SettingsFragment.getPath(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(); + } + }); + } + } + + public static String redirectUserToAuthorizeAndLogin(String clientId, String instance) { String queryString = Helper.CLIENT_ID + "="+ clientId; queryString += "&" + Helper.REDIRECT_URI + "="+ Uri.encode(Helper.REDIRECT_CONTENT_WEB);