Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9a28b5f4 authored by Nihar Thakkar's avatar Nihar Thakkar
Browse files

Check if sync is enabled/disabled and show a Snackbar

parent 166130cb
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="android.permission.READ_SYNC_SETTINGS"/>

    <application
        android:name=".util.Notes"
+35 −6
Original line number Diff line number Diff line
@@ -7,7 +7,9 @@ import android.graphics.Canvas;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
@@ -83,6 +85,8 @@ public class NotesListViewActivity extends AppCompatActivity implements ItemAdap
    RecyclerView listNavigationMenu;
    @BindView(R.id.recycler_view)
    RecyclerView listView;
    @BindView(R.id.parent)
    CoordinatorLayout coordinatorLayout;

    private ActionBarDrawerToggle drawerToggle;
    private ItemAdapter adapter = null;
@@ -140,9 +144,21 @@ public class NotesListViewActivity extends AppCompatActivity implements ItemAdap
        // refresh and sync every time the activity gets visible
        refreshLists();
        db.getNoteServerSyncHelper().addCallbackPull(syncCallBack);
        if (db.getNoteServerSyncHelper().isSyncEnabled()) {
            if (db.getNoteServerSyncHelper().isSyncPossible()) {
                synchronize();
            }
        }
        else {
            Snackbar.make(coordinatorLayout, getString(R.string.error_sync_disabled),
                    Snackbar.LENGTH_LONG).setAction(R.string.action_enable_sync, new
                    View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(new Intent(Settings.ACTION_SYNC_SETTINGS));
                }
            }).show();
        }
        super.onResume();
    }

@@ -179,6 +195,7 @@ public class NotesListViewActivity extends AppCompatActivity implements ItemAdap
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (db.getNoteServerSyncHelper().isSyncEnabled()) {
                    if (db.getNoteServerSyncHelper().isSyncPossible()) {
                        synchronize();
                    } else {
@@ -186,6 +203,18 @@ public class NotesListViewActivity extends AppCompatActivity implements ItemAdap
                        Toast.makeText(getApplicationContext(), getString(R.string.error_sync, getString(NotesClientUtil.LoginStatus.NO_NETWORK.str)), Toast.LENGTH_LONG).show();
                    }
                }
                else {
                    swipeRefreshLayout.setRefreshing(false);
                    Snackbar.make(coordinatorLayout, getString(R.string.error_sync_disabled),
                            Snackbar.LENGTH_LONG).setAction(R.string.action_enable_sync, new
                            View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    startActivity(new Intent(Settings.ACTION_SYNC_SETTINGS));
                                }
                            }).show();
                }
            }
        });

        // Floating Action Button
+64 −20
Original line number Diff line number Diff line
package it.niedermann.owncloud.notes.persistence;

import android.accounts.AccountManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
@@ -45,6 +47,10 @@ public class NoteServerSyncHelper {

    private static NoteServerSyncHelper instance;

    private static final String eelo_account_type = "bitfire.at.davdroid.eelo";
    private static final String account_email_address_key = "email_address";
    private static final String notes_content_authority = "it.niedermann.owncloud.notes.android.providers.AppContentProvider";

    /**
     * Get (or create) instance from NoteServerSyncHelper.
     * This has to be a singleton in order to realize correct registering and unregistering of
@@ -133,6 +139,42 @@ public class NoteServerSyncHelper {
        return !PreferenceManager.getDefaultSharedPreferences(context).getString(SettingsActivity.SETTINGS_URL, SettingsActivity.DEFAULT_SETTINGS).isEmpty();
    }

    private android.accounts.Account[] getEeloAccountsOnDevice(AccountManager accountManager) {
        return accountManager.getAccountsByType(
                eelo_account_type);
    }

    public boolean isSyncEnabled() {
        AccountManager accountManager = AccountManager.get(appContext);
        boolean isEeloAccount = false;

        try {
            android.accounts.Account[] eeloAccounts = getEeloAccountsOnDevice(accountManager);

            for (android.accounts.Account eeloAccount : eeloAccounts) {
                String emailId = accountManager.getUserData(eeloAccount,
                        account_email_address_key);
                if (PreferenceManager.getDefaultSharedPreferences(appContext).getString(
                        SettingsActivity.SETTINGS_USERNAME, SettingsActivity.DEFAULT_SETTINGS)
                        .equals(emailId)) {
                    isEeloAccount = true;
                    if (ContentResolver.getSyncAutomatically(eeloAccount,
                            notes_content_authority)) {
                        return true;
                    }
                }
            }
        }
        catch (SecurityException e) {
            e.printStackTrace();
        }

        if (isEeloAccount) {
            return false;
        }
        return true;
    }

    /**
     * Synchronization is only possible, if there is an active network connection and
     * Cert4Android service is available.
@@ -183,6 +225,7 @@ public class NoteServerSyncHelper {
    public void scheduleSync(boolean onlyLocalChanges) {
        Log.d(getClass().getSimpleName(), "Sync requested (" + (onlyLocalChanges ? "onlyLocalChanges" : "full") + "; " + (syncActive ? "sync active" : "sync NOT active") + ") ...");
        Log.d(getClass().getSimpleName(), "(network:" + networkConnected + "; conf:" + isConfigured(appContext) + "; cert4android:" + cert4androidReady + ")");
        if (isSyncEnabled()) {
            if (isSyncPossible() && (!syncActive || onlyLocalChanges)) {
                Log.d(getClass().getSimpleName(), "... starting now");
                SyncTask syncTask = new SyncTask(onlyLocalChanges);
@@ -206,6 +249,7 @@ public class NoteServerSyncHelper {
                }
            }
        }
    }

    private void updateNetworkStatus() {
        ConnectivityManager connMgr = (ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
+1 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

+2 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
    <string name="action_drawer_close">close navigation</string>
    <string name="action_recent">Recent</string>
    <string name="action_uncategorized">Uncategorized</string>
    <string name="action_enable_sync">Enable</string>
    <string name="menu_delete">Delete</string>
    <string name="menu_change_category">Category</string>
    <string name="menu_favorite">Favorite</string>
@@ -77,6 +78,7 @@
    <string name="error_server">URL/Server has errors</string>
    <string name="error_url_malformed">Wrong server address</string>
    <string name="error_username_password_invalid">Wrong username or password</string>
    <string name="error_sync_disabled">Sync has been disabled for this account</string>

    <!-- Snackbar Actions -->
    <string name="snackbar_settings">Settings</string>