Speed up starting

This commit is contained in:
tom79 2019-11-01 15:17:17 +01:00
parent 49acc3f977
commit 990fad0e0d
5 changed files with 135 additions and 86 deletions

View File

@ -59,6 +59,7 @@ import androidx.appcompat.widget.PopupMenu;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.Menu;
@ -197,6 +198,7 @@ public abstract class BaseMainActivity extends BaseActivity
MASTALAB
}
public static iconLauncher mLauncher = iconLauncher.BUBBLES;
private Account account;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -208,7 +210,7 @@ public abstract class BaseMainActivity extends BaseActivity
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(getApplicationContext()));
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account account = new AccountDAO(getApplicationContext(), db).getUniqAccount(userId, instance);
account = new AccountDAO(getApplicationContext(), db).getUniqAccount(userId, instance);
Intent intent = getIntent();
PackageManager pm = getPackageManager();
try {
@ -438,7 +440,7 @@ public abstract class BaseMainActivity extends BaseActivity
main_app_container = findViewById(R.id.main_app_container);
if (social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA || social == UpdateAccountInfoAsyncTask.SOCIAL.GNU || social == UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) {
new SyncTimelinesAsyncTask(BaseMainActivity.this, 0, BaseMainActivity.this).execute();
new SyncTimelinesAsyncTask(BaseMainActivity.this, 0, Helper.canFetchList(getApplicationContext(), account), BaseMainActivity.this).execute();
} else if (social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE) {
TabLayout.Tab pTabsub = tabLayout.newTab();
@ -809,7 +811,7 @@ public abstract class BaseMainActivity extends BaseActivity
int position = 0;
if (tabLayout != null)
position = tabLayout.getSelectedTabPosition();
new SyncTimelinesAsyncTask(BaseMainActivity.this, position, BaseMainActivity.this).execute();
new SyncTimelinesAsyncTask(BaseMainActivity.this, position, true,BaseMainActivity.this).execute();
}
};
LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(hidde_menu, new IntentFilter(Helper.RECEIVE_HIDE_ITEM));
@ -1543,12 +1545,13 @@ public abstract class BaseMainActivity extends BaseActivity
} else if (extras.getInt(Helper.INTENT_ACTION) == Helper.REDRAW_MENU) {
Helper.hideMenuItem(BaseMainActivity.this, navigationView.getMenu());
} else if (extras.getInt(Helper.INTENT_ACTION) == Helper.SEARCH_TAG) {
new SyncTimelinesAsyncTask(BaseMainActivity.this, -1, BaseMainActivity.this).execute();
new SyncTimelinesAsyncTask(BaseMainActivity.this, -1, false,BaseMainActivity.this).execute();
} else if (extras.getInt(Helper.INTENT_ACTION) == Helper.REFRESH_TIMELINE) {
int position = 0;
boolean refreshList = extras.getBoolean(Helper.REFRESH_LIST_TIMELINE, false);
if (tabLayout != null)
position = tabLayout.getSelectedTabPosition();
new SyncTimelinesAsyncTask(BaseMainActivity.this, position, BaseMainActivity.this).execute();
new SyncTimelinesAsyncTask(BaseMainActivity.this, position, refreshList,BaseMainActivity.this).execute();
} else if (extras.getInt(Helper.INTENT_ACTION) == Helper.SEARCH_REMOTE) {
String url = extras.getString(Helper.SEARCH_URL);
intent.replaceExtras(new Bundle());

View File

@ -98,6 +98,7 @@ public class ReorderTimelinesActivity extends BaseActivity implements OnStartDra
private String oldSearch;
private int theme;
private String instance;
private boolean refresh_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -128,7 +129,7 @@ public class ReorderTimelinesActivity extends BaseActivity implements OnStartDra
style = R.style.Dialog;
}
refresh_list = false;
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ActionBar actionBar = getSupportActionBar();
@ -404,6 +405,7 @@ public class ReorderTimelinesActivity extends BaseActivity implements OnStartDra
timeline = manageTimelines;
new ManageListsAsyncTask(getApplicationContext(), ManageListsAsyncTask.action.DELETE_LIST, null, null, manageTimelines.getListTimeline().getId(), null, ReorderTimelinesActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new TimelinesDAO(getApplicationContext(), db).remove(timeline);
refresh_list = true;
break;
}
updated = true;
@ -428,6 +430,7 @@ public class ReorderTimelinesActivity extends BaseActivity implements OnStartDra
if (updated) {
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.putExtra(Helper.INTENT_ACTION, Helper.REFRESH_TIMELINE);
intent.putExtra(Helper.REFRESH_LIST_TIMELINE, refresh_list);
startActivity(intent);
updated = false;
}

View File

@ -15,16 +15,19 @@
package app.fedilab.android.asynctasks;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import app.fedilab.android.client.API;
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.client.Entities.ManageTimelines;
import app.fedilab.android.client.Entities.RemoteInstance;
import app.fedilab.android.client.Entities.TagTimeline;
@ -32,7 +35,6 @@ import app.fedilab.android.helper.Helper;
import app.fedilab.android.sqlite.InstancesDAO;
import app.fedilab.android.sqlite.SearchDAO;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.TimelineCacheDAO;
import app.fedilab.android.sqlite.TimelinesDAO;
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.interfaces.OnSyncTimelineInterface;
@ -50,11 +52,15 @@ public class SyncTimelinesAsyncTask extends AsyncTask<Void, Void, Void> {
private int position;
private WeakReference<Context> contextReference;
private List<ManageTimelines> manageTimelines;
private boolean syncLists;
public SyncTimelinesAsyncTask(Context context, int position, OnSyncTimelineInterface onSyncTimelineInterface) {
public SyncTimelinesAsyncTask(Context context, int position, boolean syncLists, OnSyncTimelineInterface onSyncTimelineInterface) {
this.contextReference = new WeakReference<>(context);
this.listener = onSyncTimelineInterface;
this.position = position;
this.syncLists = syncLists;
}
@ -245,88 +251,97 @@ public class SyncTimelinesAsyncTask extends AsyncTask<Void, Void, Void> {
}
}
}
APIResponse apiResponse;
if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA) {
List<app.fedilab.android.client.Entities.List> listsAPI = null;
try {
apiResponse = new API(contextReference.get()).getLists();
listsAPI = apiResponse.getLists();
//Check potential duplicated lists in db
List<ManageTimelines> duplicated_id = new ArrayList<>();
List<String> present_id = new ArrayList<>();
for (ManageTimelines manageTimeline : manageTimelines) {
if (manageTimeline.getListTimeline() == null)
continue;
if (!present_id.contains(manageTimeline.getListTimeline().getId())) {
present_id.add(manageTimeline.getListTimeline().getId());
} else {
duplicated_id.add(manageTimeline);
new TimelinesDAO(contextReference.get(), db).remove(manageTimeline);
}
}
manageTimelines.removeAll(duplicated_id);
if (listsAPI != null && listsAPI.size() > 0) {
//Loop through results
for (app.fedilab.android.client.Entities.List list : listsAPI) {
boolean isInDb = false;
ManageTimelines timelines_tmp = null;
for (ManageTimelines manageTimeline : manageTimelines) {
if (manageTimeline.getListTimeline() == null)
continue;
if (manageTimeline.getListTimeline().getId().equals(list.getId())) {
isInDb = true;
timelines_tmp = manageTimeline;
break;
}
}
//The current list is not registred in the database
if (!isInDb) {
ManageTimelines manageTL = new ManageTimelines();
manageTL.setListTimeline(list);
manageTL.setDisplayed(true);
manageTL.setType(ManageTimelines.Type.LIST);
manageTL.setPosition(manageTimelines.size());
new TimelinesDAO(contextReference.get(), db).insert(manageTL);
manageTimelines.add(manageTL);
} else {
//Update list
timelines_tmp.setListTimeline(list);
new TimelinesDAO(contextReference.get(), db).update(timelines_tmp);
}
}
ArrayList<ManageTimelines> manageTimelinesToRemove = new ArrayList<>();
for (ManageTimelines dbtTimelines : manageTimelines) {
if (dbtTimelines.getListTimeline() == null)
if( this.syncLists ) {
APIResponse apiResponse;
if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA) {
List<app.fedilab.android.client.Entities.List> listsAPI;
try {
apiResponse = new API(contextReference.get()).getLists();
listsAPI = apiResponse.getLists();
//Check potential duplicated lists in db
List<ManageTimelines> duplicated_id = new ArrayList<>();
List<String> present_id = new ArrayList<>();
for (ManageTimelines manageTimeline : manageTimelines) {
if (manageTimeline.getListTimeline() == null)
continue;
boolean shouldBeRemoved = true;
if (!present_id.contains(manageTimeline.getListTimeline().getId())) {
present_id.add(manageTimeline.getListTimeline().getId());
} else {
duplicated_id.add(manageTimeline);
new TimelinesDAO(contextReference.get(), db).remove(manageTimeline);
}
}
manageTimelines.removeAll(duplicated_id);
if (listsAPI != null && listsAPI.size() > 0) {
//Loop through results
for (app.fedilab.android.client.Entities.List list : listsAPI) {
if (list.getId().equals(dbtTimelines.getListTimeline().getId())) {
shouldBeRemoved = false;
boolean isInDb = false;
ManageTimelines timelines_tmp = null;
for (ManageTimelines manageTimeline : manageTimelines) {
if (manageTimeline.getListTimeline() == null)
continue;
if (manageTimeline.getListTimeline().getId().equals(list.getId())) {
isInDb = true;
timelines_tmp = manageTimeline;
break;
}
}
//The current list is not registred in the database
if (!isInDb) {
ManageTimelines manageTL = new ManageTimelines();
manageTL.setListTimeline(list);
manageTL.setDisplayed(true);
manageTL.setType(ManageTimelines.Type.LIST);
manageTL.setPosition(manageTimelines.size());
new TimelinesDAO(contextReference.get(), db).insert(manageTL);
manageTimelines.add(manageTL);
} else {
//Update list
timelines_tmp.setListTimeline(list);
new TimelinesDAO(contextReference.get(), db).update(timelines_tmp);
}
}
if (shouldBeRemoved) {
new TimelinesDAO(contextReference.get(), db).remove(dbtTimelines);
manageTimelinesToRemove.add(dbtTimelines);
}
}
if (manageTimelinesToRemove.size() > 0) {
manageTimelines.removeAll(manageTimelinesToRemove);
}
} else { //No lists, all are removed if exist in db
ArrayList<ManageTimelines> manageTimelinesToRemove = new ArrayList<>();
if (apiResponse.getError() == null) { //Only done if there is no errors when fetching lists
for (ManageTimelines manageTimelines : manageTimelines) {
if (manageTimelines.getListTimeline() == null)
ArrayList<ManageTimelines> manageTimelinesToRemove = new ArrayList<>();
for (ManageTimelines dbtTimelines : manageTimelines) {
if (dbtTimelines.getListTimeline() == null)
continue;
new TimelinesDAO(contextReference.get(), db).remove(manageTimelines);
manageTimelinesToRemove.add(manageTimelines);
boolean shouldBeRemoved = true;
for (app.fedilab.android.client.Entities.List list : listsAPI) {
if (list.getId().equals(dbtTimelines.getListTimeline().getId())) {
shouldBeRemoved = false;
}
}
if (shouldBeRemoved) {
new TimelinesDAO(contextReference.get(), db).remove(dbtTimelines);
manageTimelinesToRemove.add(dbtTimelines);
}
}
if (manageTimelinesToRemove.size() > 0) {
manageTimelines.removeAll(manageTimelinesToRemove);
}
} else { //No lists, all are removed if exist in db
ArrayList<ManageTimelines> manageTimelinesToRemove = new ArrayList<>();
if (apiResponse.getError() == null) { //Only done if there is no errors when fetching lists
for (ManageTimelines manageTimelines : manageTimelines) {
if (manageTimelines.getListTimeline() == null)
continue;
new TimelinesDAO(contextReference.get(), db).remove(manageTimelines);
manageTimelinesToRemove.add(manageTimelines);
}
if (manageTimelinesToRemove.size() > 0) {
manageTimelines.removeAll(manageTimelinesToRemove);
}
}
}
SharedPreferences sharedpreferences = contextReference.get().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = Helper.getLiveInstance(contextReference.get());
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_DATE_LIST_FETCH+userId+instance, Helper.dateToString(new Date()));
editor.apply();
} catch (Exception ignored) {
}
} catch (Exception ignored) {
}
}
for (Iterator<ManageTimelines> it = manageTimelines.iterator(); it.hasNext(); ) {

View File

@ -3969,13 +3969,27 @@ public class API {
lists = parseLists(new JSONArray(response));
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException e) {
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (JSONException e) {
}
apiResponse.setLists(lists);
return apiResponse;
}
/**
* Get lists for the user
*
* @return APIResponse
*/
public APIResponse getListsRemote(String prefKeyOauthTokenT) {
apiResponse = new APIResponse();
List<app.fedilab.android.client.Entities.List> lists = new ArrayList<>();
try {
String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/lists"), 5, null, prefKeyOauthTokenT);
lists = parseLists(new JSONArray(response));
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
e.printStackTrace();
}
apiResponse.setLists(lists);

View File

@ -200,6 +200,7 @@ import javax.net.ssl.SSLContext;
import app.fedilab.android.BuildConfig;
import app.fedilab.android.activities.MutedInstanceActivity;
import app.fedilab.android.asynctasks.PostActionAsyncTask;
import app.fedilab.android.asynctasks.WhoToFollowAsyncTask;
import app.fedilab.android.client.API;
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Account;
@ -215,6 +216,7 @@ import app.fedilab.android.client.Entities.Status;
import app.fedilab.android.client.Entities.Tag;
import app.fedilab.android.client.Entities.TagTimeline;
import app.fedilab.android.client.Tls12SocketFactory;
import app.fedilab.android.fragments.WhoToFollowFragment;
import app.fedilab.android.services.LiveNotificationDelayedService;
import app.fedilab.android.services.LiveNotificationService;
import app.fedilab.android.sqlite.MainMenuDAO;
@ -319,7 +321,7 @@ public class Helper {
public static final String LAST_DATE_LIST_NAME_REFRESH = "last_date_list_name_refresh";
public static final String LAST_LIST = "last_list";
public static final String LAST_LIST_NAME = "last_list_name";
public static final String LAST_DATE_LIST_FETCH = "last_date_list_fetch";
//Notifications
public static final int NOTIFICATION_INTENT = 1;
@ -508,6 +510,7 @@ public class Helper {
public static final String INTENT_BACKUP_FINISH = "intent_backup_finish";
public static final String INTENT_SEND_MODIFIED_IMAGE = "intent_send_modified_image";
public static final String INTENT_ADD_UPLOADED_MEDIA = "intent_add_uploaded_media";
public static final String REFRESH_LIST_TIMELINE = "refresh_list_timeline";
//Receiver
public static final String RECEIVE_DATA = "receive_data";
public static final String RECEIVE_ACTION = "receive_action";
@ -4977,4 +4980,15 @@ public class Helper {
}
public static boolean canFetchList(Context context, Account account){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String lastDateListRefresh = sharedpreferences.getString(Helper.LAST_DATE_LIST_FETCH+account.getId()+account.getInstance(), null);
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.HOUR, -24);
Date dateAllowed = cal.getTime();
Date lastRefresh = Helper.stringToDate(context, lastDateListRefresh);
return (lastRefresh == null || lastRefresh.before(dateAllowed));
}
}