Skip to content

Commit

Permalink
bring back periodic sync
Browse files Browse the repository at this point in the history
  • Loading branch information
guanlisheng committed Jan 10, 2024
1 parent 41c58f7 commit 94b7b68
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 164 deletions.
26 changes: 24 additions & 2 deletions app/src/main/java/com/money/manager/ex/MmexApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import androidx.preference.PreferenceManager;
import android.text.TextUtils;
import android.widget.TextView;

import androidx.multidex.MultiDexApplication;
import androidx.preference.PreferenceManager;

import com.amplitude.android.Amplitude;
import com.amplitude.android.AmplitudeKt;
import com.amplitude.android.DefaultTrackingOptions;
import com.evernote.android.job.JobManager;
import com.mikepenz.iconics.Iconics;
import com.mikepenz.mmex_icon_font_typeface_library.MMXIconFont;
Expand Down Expand Up @@ -52,8 +58,8 @@
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;

import androidx.multidex.MultiDexApplication;
import info.javaperformance.money.Money;
import kotlin.Unit;
import timber.log.Timber;

/**
Expand All @@ -67,6 +73,7 @@
public class MmexApplication
extends MultiDexApplication {

static private Amplitude mAmplitude;
private static MmexApplication appInstance;
private static float mTextSize;
private static String userName = "";
Expand All @@ -75,6 +82,11 @@ public static MmexApplication getApp() {
return appInstance;
}

public static Amplitude getAmplitude()
{
return mAmplitude;
}

public static float getTextSize() {
return MmexApplication.mTextSize;
}
Expand Down Expand Up @@ -132,6 +144,16 @@ public void onCreate() {
initializeJobManager();

getOrCreateUUID(this);

mAmplitude = AmplitudeKt.Amplitude("1e1fbc10354400d9c3392a89558d693d"
, getApplicationContext()
, configuration -> {
configuration.setDefaultTracking(DefaultTrackingOptions.ALL);;
return Unit.INSTANCE;
}
);

mAmplitude.setDeviceId(getOrCreateUUID(getApplicationContext()));
}

public static String getOrCreateUUID(Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,35 @@

import androidx.appcompat.app.AppCompatActivity;

import com.amplitude.android.Amplitude;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.money.manager.ex.MmexApplication;
import com.money.manager.ex.core.RequestCodes;
import com.money.manager.ex.core.database.DatabaseManager;
import com.money.manager.ex.home.DatabaseMetadata;
import com.money.manager.ex.utils.MmxDatabaseUtils;
import com.money.manager.ex.utils.MmxDate;

import com.amplitude.android.Amplitude;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;

import timber.log.Timber;

/**
* Functions to assist with selecting database file.
*/
public class FileStorageHelper {
public FileStorageHelper(AppCompatActivity host) {
public FileStorageHelper(Context host) {
_host = host;
}

private final AppCompatActivity _host;
private final Context _host;

public Context getContext() {
return _host;
Expand All @@ -54,8 +57,7 @@ public Context getContext() {
public void showStorageFilePicker() {
// show the file picker
int requestCode = RequestCodes.SELECT_DOCUMENT;
AppCompatActivity host = _host;

AppCompatActivity host = (AppCompatActivity) _host;
try {
// ACTION_GET_CONTENT in older versions of Android.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
Expand All @@ -73,8 +75,7 @@ public void showStorageFilePicker() {
public void showCreateFilePicker() {
// show the file picker
int requestCode = RequestCodes.CREATE_DOCUMENT;
AppCompatActivity host = _host;

AppCompatActivity host = (AppCompatActivity) _host;
try {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
Expand Down Expand Up @@ -155,7 +156,7 @@ public String synchronize(DatabaseMetadata metadata) {
Private area
*/

private boolean isLocalFileChanged(DatabaseMetadata metadata) {
public boolean isLocalFileChanged(DatabaseMetadata metadata) {
MmxDate localLastModifiedMmxDate = getLocalFileModifiedDate(metadata);
Date localLastModified = localLastModifiedMmxDate.toDate();
// The timestamp when the local file was downloaded.
Expand All @@ -165,7 +166,7 @@ private boolean isLocalFileChanged(DatabaseMetadata metadata) {
return result;
}

private boolean isRemoteFileChanged(DatabaseMetadata metadata) {
public boolean isRemoteFileChanged(DatabaseMetadata metadata) {
DocFileMetadata remote = getRemoteMetadata(metadata);

// Check if the remote file was modified since fetched.
Expand All @@ -181,7 +182,7 @@ private boolean isRemoteFileChanged(DatabaseMetadata metadata) {
* Copies the remote database locally and updates the metadata.
* @param metadata Database file metadata.
*/
private void pullDatabase(DatabaseMetadata metadata) {
public void pullDatabase(DatabaseMetadata metadata) {
// copy the contents into a local database file.
Uri uri = Uri.parse(metadata.remotePath);
try {
Expand Down Expand Up @@ -209,13 +210,18 @@ private void pullDatabase(DatabaseMetadata metadata) {
Toast.makeText(getContext(),"Unable to open DB. Not a .mmb file.", Toast.LENGTH_SHORT).show();
return;
}
dbUtils.useDatabase(metadata);
MmexApplication.getAmplitude().track("synchronize", new HashMap() {{
put("authority", uri.getAuthority());
put("result", "pullDatabase");
}});
}

/**
* Pushes the local file to the document provider and updates the metadata.
* @param metadata Database file metadata.
*/
private void pushDatabase(DatabaseMetadata metadata) {
public void pushDatabase(DatabaseMetadata metadata) {
// upload local file
uploadDatabase(metadata);

Expand All @@ -236,6 +242,11 @@ private void pushDatabase(DatabaseMetadata metadata) {
metadata.localSnapshotTimestamp = localLastModifiedMmxDate.toIsoString();

saveMetadata(metadata);

MmexApplication.getAmplitude().track("synchronize", new HashMap() {{
put("authority", remoteUri.getAuthority());
put("result", "pushDatabase");
}});
}

/**
Expand Down Expand Up @@ -284,7 +295,7 @@ private DocFileMetadata getRemoteMetadata(DatabaseMetadata metadata) {
}

private DocFileMetadata getRemoteMetadata(Uri uri) {
AppCompatActivity host = _host;
Context host = _host;

DocFileMetadata result = new DocFileMetadata();
result.Uri = uri.toString();
Expand Down Expand Up @@ -399,7 +410,7 @@ private void downloadDatabase(Uri uri, String localPath) throws Exception {
*/
private void showSelectLocalFileDialog() {
int requestCode = RequestCodes.SELECT_FILE;
AppCompatActivity host = _host;
AppCompatActivity host = (AppCompatActivity) _host;

//MmxDatabaseUtils dbUtils = new MmxDatabaseUtils(host);
DatabaseManager dbManager = new DatabaseManager(getContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public String getFileName() {
File file = new File(this.localPath);
return file.getName();
}

public boolean isSynchronised() {
return !TextUtils.isEmpty(remotePath);
}
Expand Down
35 changes: 6 additions & 29 deletions app/src/main/java/com/money/manager/ex/home/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ public class MainActivity

public static final String EXTRA_DATABASE_PATH = "dbPath";
public static final String EXTRA_SKIP_REMOTE_CHECK = "skipRemoteCheck";
private Amplitude mAmplitude;
/**
* @return the mRestart
*/
Expand Down Expand Up @@ -184,13 +183,7 @@ protected void onCreate(Bundle savedInstanceState) {
finish();
return;
}
mAmplitude = AmplitudeKt.Amplitude("1e1fbc10354400d9c3392a89558d693d"
, getApplicationContext()
, configuration -> {
configuration.setDefaultTracking(DefaultTrackingOptions.ALL);;
return Unit.INSTANCE;
}
);
Amplitude amplitude = MmexApplication.getAmplitude();
// todo: remove this after the users upgrade the recent files list.
migrateRecentDatabases();

Expand Down Expand Up @@ -236,10 +229,8 @@ protected void onCreate(Bundle savedInstanceState) {
.atZone(ZoneId.of("UTC"))
.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
}
mAmplitude.setUserId(uid);
mAmplitude.setDeviceId(MmexApplication.getOrCreateUUID(getApplicationContext()));
amplitude.setUserId(uid);

onceSynchronize();
infoService.setInfoValue(InfoKeys.UID, uid);

// fragments
Expand Down Expand Up @@ -606,7 +597,10 @@ public boolean onDrawerMenuAndOptionMenuSelected(DrawerMenuItem item) {
showFragment(HomeFragment.class);
break;
case R.id.menu_sync:
onceSynchronize();
SyncManager sync = new SyncManager(this);
sync.triggerSynchronization();
// re-set the sync timer.
sync.startSyncServiceHeartbeat();
break;

case R.id.menu_open_database:
Expand Down Expand Up @@ -1362,23 +1356,6 @@ private void showCurrentDatabasePath(Context context) {
}
}

private void onceSynchronize() {
FileStorageHelper storage = new FileStorageHelper(this);
DatabaseMetadata current = mDatabases.get().getCurrent();
String result = storage.synchronize(current);

logSynchronize(current, result);
}

private void logSynchronize(DatabaseMetadata metadata, String result) {
Uri uri = Uri.parse(metadata.remotePath);
String authority = uri.getAuthority();
mAmplitude.track("synchronize", new HashMap() {{
put("authority", authority);
put("result", result);
}});
}

private void showFragment_Internal(Fragment fragment, String tag) {
// Check if fragment is already added.
if (fragment.isAdded()) return;
Expand Down
34 changes: 13 additions & 21 deletions app/src/main/java/com/money/manager/ex/sync/SyncManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Messenger;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.evernote.android.job.JobManager;
import com.google.common.io.ByteStreams;
import com.money.manager.ex.Constants;
import com.money.manager.ex.MmexApplication;
import com.money.manager.ex.R;
Expand All @@ -48,20 +50,12 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.HashMap;

import javax.inject.Inject;

import androidx.appcompat.app.AppCompatActivity;
import dagger.Lazy;
import rx.Single;
import rx.functions.Action1;
import timber.log.Timber;

/**
Expand Down Expand Up @@ -270,15 +264,15 @@ public void invokeSyncService(String action) {
// progressDialog.show();
}

DatabaseMetadata current = mDatabases.get().getCurrent();
String localFile = getDatabases().getCurrent().localPath;
Messenger messenger = null;
if (getContext() instanceof AppCompatActivity) {
// Messenger handles received messages from the sync service. Can run only in a looper thread.
messenger = new Messenger(new SyncServiceMessageHandler(getContext(), progressDialog, remoteFile));
messenger = new Messenger(new SyncServiceMessageHandler(getContext(), progressDialog));
}

Intent syncServiceIntent = IntentFactory.getSyncServiceIntent(getContext(), action,
localFile, remoteFile, messenger);
Intent syncServiceIntent = IntentFactory.getSyncServiceIntent(getContext(), action, current.localPath, current.remotePath, messenger);
// start service
SyncService.enqueueWork(getContext(), syncServiceIntent);

Expand Down Expand Up @@ -435,14 +429,12 @@ public void triggerSynchronization() {
return;
}

// easy comparison, just by the file name.
if (!areFileNamesSame(localPath, remotePath)) {
// The current file was probably opened through Open Database.
Toast.makeText(getContext(), R.string.db_not_dropbox, Toast.LENGTH_LONG).show();
return;
}

invokeSyncService(SyncConstants.INTENT_ACTION_SYNC);
Uri uri = Uri.parse(remotePath);
MmexApplication.getAmplitude().track("synchronize", new HashMap() {{
put("authority", uri.getAuthority());
put("result", "triggerSynchronization");
}});
}

public void triggerDownload() {
Expand Down Expand Up @@ -685,7 +677,7 @@ private PendingIntent getPendingIntentForDelayedUpload() {

return PendingIntent.getService(getContext(),
SyncConstants.REQUEST_DELAYED_SYNC,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
intent, PendingIntent.FLAG_CANCEL_CURRENT|PendingIntent.FLAG_IMMUTABLE);
}

private AlarmManager getAlarmManager() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void onReceive(Context context, Intent intent) {

Intent syncIntent = new Intent(context, SyncBroadcastReceiver.class);
PendingIntent pendingSyncIntent = PendingIntent.getBroadcast(context, SyncConstants.REQUEST_PERIODIC_SYNC,
syncIntent, PendingIntent.FLAG_UPDATE_CURRENT);
syncIntent, PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_IMMUTABLE);
// PendingIntent.FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT

AlarmManager alarmManager = getAlarmManager(context);
Expand Down
Loading

0 comments on commit 94b7b68

Please sign in to comment.