Skip to content

Commit

Permalink
Enable dropbox sync and show more accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
spacecowboy committed Nov 7, 2015
1 parent 6281f93 commit b245b99
Show file tree
Hide file tree
Showing 16 changed files with 307 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

package com.nononsenseapps.notepad.sync.orgsync;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;

/**
* Dummy file, see play flavor.
*/
public class DropboxSyncHelper {

public DropboxSyncHelper(Activity activity) {
public DropboxSyncHelper(Context context) {

}

Expand All @@ -43,4 +44,8 @@ public void unlinkAccount() {
public boolean handleLinkResult() {
return false;
}

public @NonNull String getAccount() {
return "";
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/nononsenseapps/helpers/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

package com.nononsenseapps.helpers;

import com.nononsenseapps.build.Config;
import com.nononsenseapps.notepad.BuildConfig;

public final class Log {

public static void d(String t, String m) {
if (Config.LOGGING || true)
if (BuildConfig.DEBUG)
android.util.Log.d(t, m);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public void onClick(View view) {
NotificationHelper.schedule(this);
// Schedule syncs
BackgroundSyncScheduler.scheduleSync(this);
// Sync if appropriate
OrgSyncService.start(this);
}

@Override
Expand All @@ -96,9 +98,6 @@ public void onDestroy() {
@Override
protected void onResume() {
super.onResume();

// Sync if appropriate
OrgSyncService.start(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,16 @@
import android.widget.Toast;

import com.nononsenseapps.build.Config;
import com.nononsenseapps.filepicker.DropboxFilePickerActivity;
import com.nononsenseapps.filepicker.FilePickerActivity;
import com.nononsenseapps.notepad.BuildConfig;
import com.nononsenseapps.notepad.R;
import com.nononsenseapps.notepad.legacy.Backup;
import com.nononsenseapps.notepad.sync.orgsync.DropboxSyncHelper;
import com.nononsenseapps.notepad.sync.orgsync.DropboxSynchronizer;
import com.nononsenseapps.notepad.sync.orgsync.OrgSyncService;
import com.nononsenseapps.notepad.sync.orgsync.SDSynchronizer;
import com.nononsenseapps.util.AsyncTaskHelper;
import com.nononsenseapps.util.PermissionsHelper;
import com.nononsenseapps.util.PreferenceHelper;
import com.nononsenseapps.util.SharedPreferencesHelper;
Expand All @@ -54,7 +59,9 @@

import static com.nononsenseapps.util.PermissionsHelper.hasPermissions;
import static com.nononsenseapps.util.PermissionsHelper.permissionsGranted;
import static com.nononsenseapps.util.SharedPreferencesHelper.disableDropboxSync;
import static com.nononsenseapps.util.SharedPreferencesHelper.disableSdCardSync;
import static com.nononsenseapps.util.SharedPreferencesHelper.getDropboxDir;
import static com.nononsenseapps.util.SharedPreferencesHelper.getSdDir;

/**
Expand All @@ -64,10 +71,14 @@ public class FragmentSettings extends PreferenceFragment implements SharedPrefer
.OnSharedPreferenceChangeListener {

private static final int ACTIVITY_CODE_PICK_SD_DIR = 1;
private static final int ACTIVITY_CODE_PICK_DROPBOX_DIR = 2;
private static final int PERMISSION_CODE_GTASKS = 1;
private static final int PERMISSION_CODE_SDCARD = 2;
private SwitchPreference preferenceSdDir;
private SwitchPreference preferenceSyncSdCard;
private SwitchPreference preferenceSyncGTasks;
private SwitchPreference preferenceSyncDropbox;

private DropboxSyncHelper mDropboxHelper = null;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -86,6 +97,7 @@ public void onCreate(Bundle savedInstanceState) {
buildGuard();
setupAccount(sharedPreferences);
setupDirectory(sharedPreferences);
setupDropbox(sharedPreferences);
setupPassword();
setupLegacyBackup();

Expand All @@ -112,6 +124,13 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
disableSdCardSync(getActivity());
}
break;
case ACTIVITY_CODE_PICK_DROPBOX_DIR:
if (resultCode == Activity.RESULT_OK) {
saveNewDropboxPath(data);
} else {
disableDropboxSync(getActivity());
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
Expand All @@ -125,11 +144,21 @@ private void buildGuard() {
}

private void setupDirectory(final SharedPreferences sharedPreferences) {
preferenceSdDir = (SwitchPreference) findPreference(getString(R.string
preferenceSyncSdCard = (SwitchPreference) findPreference(getString(R.string
.const_preference_sdcard_enabled_key));
setSdDirectorySummary(sharedPreferences);
}

private void setupDropbox(final SharedPreferences sharedPreferences) {
preferenceSyncDropbox = (SwitchPreference) findPreference(getString(R.string
.const_preference_dropbox_enabled_key));
setDropboxDirectorySummary(sharedPreferences);

preferenceSyncDropbox.setEnabled((BuildConfig.DROPBOX_ENABLED && Config.getKeyDropboxAPI
(getActivity()) != null &&
!Config.getKeyDropboxAPISecret(getActivity()).contains(" ")));
}

private void showFilePicker() {
if (hasSDCardPermissions()) {
// Start the filepicker
Expand All @@ -143,11 +172,37 @@ private void showFilePicker() {
}
}

private void showDropboxFilePicker() {
if (mDropboxHelper == null) {
mDropboxHelper = new DropboxSyncHelper(getActivity());
}
if (mDropboxHelper.isLinked()) {
saveDropboxAccountInBackground();
// Start the filepicker
Intent i = new Intent(getActivity(), DropboxFilePickerActivity.class);

i.putExtra(FilePickerActivity.EXTRA_START_PATH, getDropboxDir(getActivity()))
.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false).putExtra
(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, true).putExtra(FilePickerActivity
.EXTRA_MODE, FilePickerActivity.MODE_DIR);

startActivityForResult(i, ACTIVITY_CODE_PICK_DROPBOX_DIR);
} else {
mDropboxHelper.linkAccount();
// See onResume for result
}
}

private void setSdDirectorySummary(final SharedPreferences sharedPreferences) {
preferenceSdDir.setSummary(sharedPreferences.getString(getSdDirectoryKey(),
preferenceSyncSdCard.setSummary(sharedPreferences.getString(getSdDirectoryKey(),
SDSynchronizer.DEFAULT_ORG_DIR));
}

private void setDropboxDirectorySummary(final SharedPreferences sharedPreferences) {
preferenceSyncDropbox.setSummary(sharedPreferences.getString(getDropboxDirectoryKey(),
DropboxSynchronizer.DEFAULT_DIR));
}

@SuppressLint("CommitPrefEdits")
private void saveNewDirectoryPath(Intent data) {
File path = new File(data.getData().getPath());
Expand All @@ -163,10 +218,23 @@ private void saveNewDirectoryPath(Intent data) {
}
}

@SuppressLint("CommitPrefEdits")
private void saveNewDropboxPath(Intent data) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences
(getActivity());
sharedPreferences.edit().putString(getDropboxDirectoryKey(), data.getData().getPath())
.commit();
setDropboxDirectorySummary(sharedPreferences);
}

private String getSdDirectoryKey() {
return getString(R.string.const_preference_sdcard_dir_key);
}

private String getDropboxDirectoryKey() {
return getString(R.string.const_preference_dropbox_dir_key);
}

private void setupAccount(SharedPreferences sharedPreferences) {
preferenceSyncGTasks = (SwitchPreference) findPreference(getString(R.string
.const_preference_gtask_enabled_key));
Expand Down Expand Up @@ -201,15 +269,6 @@ private void requestSDCardPermissions() {
requestPermissions(PermissionsHelper.PERMISSIONS_SD, PERMISSION_CODE_SDCARD);
}

boolean allEqual(int value, int[] items) {
for (int item: items) {
if (value != item) {
return false;
}
}
return true;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]
grantResults) {
Expand Down Expand Up @@ -238,6 +297,32 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}
}

@Override
public void onResume() {
super.onResume();

if (mDropboxHelper != null && !mDropboxHelper.isLinked()) {
if (mDropboxHelper.handleLinkResult()) {
// Success, show file picker
showDropboxFilePicker();
} else {
// Link failed or was cancelled by the user.
SharedPreferencesHelper.disableDropboxSync(getActivity());
}
}
}

private void saveDropboxAccountInBackground() {
// Save account name. Needs to be done in background
AsyncTaskHelper.background(new AsyncTaskHelper.Job() {
@Override
public void doInBackground() {
SharedPreferencesHelper.setDropboxAccount(getActivity(),
mDropboxHelper.getAccount());
}
});
}

private boolean hasGoogleAccountPermissions() {
return hasPermissions(getActivity(), PermissionsHelper.PERMISSIONS_GTASKS);
}
Expand Down Expand Up @@ -361,8 +446,29 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
// Restart the service (started in activities)
OrgSyncService.stop(getActivity());
// Synchronize view also
if (preferenceSdDir.isChecked()) {
preferenceSdDir.setChecked(false);
if (preferenceSyncSdCard.isChecked()) {
preferenceSyncSdCard.setChecked(false);
}
}
} else if (key.equals(getString(R.string.const_preference_dropbox_enabled_key))) {
if (!(BuildConfig.DROPBOX_ENABLED && Config.getKeyDropboxAPI(getActivity()) !=
null &&
!Config.getKeyDropboxAPISecret(getActivity()).contains(" "))) {
return;
}

final boolean enabled = sharedPreferences.getBoolean(getString(R.string
.const_preference_dropbox_enabled_key), false);

if (enabled) {
showDropboxFilePicker();
} else {
SharedPreferencesHelper.disableDropboxSync(getActivity());
// Restart the service (started in activities)
OrgSyncService.stop(getActivity());
// Synchronize view also
if (preferenceSyncDropbox.isChecked()) {
preferenceSyncDropbox.setChecked(false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.nononsenseapps.notepad.database.Task;
import com.nononsenseapps.notepad.database.TaskList;
import com.nononsenseapps.util.SharedPreferencesHelper;
import com.nononsenseapps.util.SyncGtaskHelper;

import java.util.ArrayList;

Expand Down Expand Up @@ -474,7 +475,23 @@ public int getViewType() {
class TopLevelItem implements ExtraItem {

public String getAvatarName() {
return SharedPreferencesHelper.getGoogleAccount(getActivity());
// Try google account first
String result = "";
if (SyncGtaskHelper.isGTasksConfigured(getActivity())) {
result = SharedPreferencesHelper.getGoogleAccount(getActivity());
}

if (result.isEmpty() && SharedPreferencesHelper.isDropboxSyncEnabled(getActivity())) {
// Then try dropbox
result = SharedPreferencesHelper.getDropboxAccount(getActivity());
}

if (result.isEmpty() && SharedPreferencesHelper.isSdSyncEnabled(getActivity())) {
// Then try folder
result = SharedPreferencesHelper.getSdDir(getActivity());
}

return result;
}

@Override
Expand Down Expand Up @@ -606,7 +623,7 @@ public TopLevelItemViewHolder(View itemView) {

public void bind(TopLevelItem topLevelItem) {
final String name = topLevelItem.getAvatarName();
final String imageName = name.isEmpty() ? "N" : name;
final String imageName = (name.isEmpty() || name.startsWith("/")) ? "N" : name;
TextDrawable drawable = TextDrawable.builder()
.buildRound(imageName.toUpperCase().substring(0, 1), ColorGenerator.MATERIAL
.getColor(imageName));
Expand Down Expand Up @@ -720,7 +737,5 @@ public void onClick(View v) {
mCallbacks.openList(id);
mDrawerLayout.closeDrawers();
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import android.os.Process;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.nononsenseapps.helpers.Log;
import com.nononsenseapps.notepad.BuildConfig;
import com.nononsenseapps.notepad.database.Task;
import com.nononsenseapps.notepad.database.TaskList;
Expand Down Expand Up @@ -113,7 +113,7 @@ public ArrayList<SynchronizerInterface> getSynchronizers() {
}

// Try Dropbox
if (BuildConfig.DROPBOX_ENABLED && BuildConfig.NONFREE) {
if (BuildConfig.DROPBOX_ENABLED) {
SynchronizerInterface db = new DropboxSynchronizer(this);
if (db.isConfigured()) {
syncers.add(db);
Expand Down
Loading

0 comments on commit b245b99

Please sign in to comment.