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

Commit 4a7554af authored by Makoto Onuki's avatar Makoto Onuki
Browse files

Add periodic sync support to requestsync.

Test: manual test
Change-Id: Ie53249de5e9c5e53bdf87cb9f78745c994bc34e6
parent a94b152d
Loading
Loading
Loading
Loading
+91 −29
Original line number Original line Diff line number Diff line
@@ -29,10 +29,41 @@ public class RequestSync {
    private int mNextArg;
    private int mNextArg;
    private String mCurArgData;
    private String mCurArgData;


    enum Operation {
        REQUEST_SYNC {
            @Override
            void invoke(RequestSync caller) {
                ContentResolver.requestSync(caller.mAccount, caller.mAuthority, caller.mExtras);
            }
        },
        ADD_PERIODIC_SYNC {
            @Override
            void invoke(RequestSync caller) {
                ContentResolver.addPeriodicSync(caller.mAccount, caller.mAuthority, caller.mExtras,
                        caller.mPeriodicIntervalSeconds);
            }
        },
        REMOVE_PERIODIC_SYNC {
            @Override
            void invoke(RequestSync caller) {
                ContentResolver.removePeriodicSync(
                        caller.mAccount, caller.mAuthority, caller.mExtras);
            }
        };

        abstract void invoke(RequestSync caller);
    }

    private Operation mOperation;

    // account & authority
    // account & authority
    private String mAccountName = null;
    private String mAccountName;
    private String mAccountType = null;
    private String mAccountType;
    private String mAuthority = null;
    private String mAuthority;

    private Account mAccount;

    private int mPeriodicIntervalSeconds;


    // extras
    // extras
    private Bundle mExtras = new Bundle();
    private Bundle mExtras = new Bundle();
@@ -80,11 +111,28 @@ public class RequestSync {
                }
                }
            }
            }


            ContentResolver.requestSync(account, mAuthority, mExtras);
            mAccount = account;

            mOperation.invoke(this);
        }
        }
    }
    }


    private boolean parseArgs() throws URISyntaxException {
    private boolean parseArgs() throws URISyntaxException {
        mOperation = Operation.REQUEST_SYNC;
        if (mArgs.length > 0) {
            switch (mArgs[0]) {
                case "add-periodic":
                    mNextArg++;
                    mOperation = Operation.ADD_PERIODIC_SYNC;
                    mPeriodicIntervalSeconds = Integer.parseInt(nextArgRequired());
                    break;
                case "remove-periodic":
                    mNextArg++;
                    mOperation = Operation.REMOVE_PERIODIC_SYNC;
                    break;
            }
        }

        String opt;
        String opt;
        while ((opt=nextOption()) != null) {
        while ((opt=nextOption()) != null) {
            if (opt.equals("-h") || opt.equals("--help")) {
            if (opt.equals("-h") || opt.equals("--help")) {
@@ -114,6 +162,8 @@ public class RequestSync {
                mExtras.putBoolean(ContentResolver.SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS, true);
                mExtras.putBoolean(ContentResolver.SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS, true);
            } else if (opt.equals("-u") || opt.equals("--upload-only")) {
            } else if (opt.equals("-u") || opt.equals("--upload-only")) {
                mExtras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, true);
                mExtras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, true);
            } else if (opt.equals("--rc") || opt.equals("--require-charging")) {
                mExtras.putBoolean(ContentResolver.SYNC_EXTRAS_REQUIRE_CHARGING, true);
            } else if (opt.equals("-e") || opt.equals("--es") || opt.equals("--extra-string")) {
            } else if (opt.equals("-e") || opt.equals("--es") || opt.equals("--extra-string")) {
                final String key = nextArgRequired();
                final String key = nextArgRequired();
                final String value = nextArgRequired();
                final String value = nextArgRequired();
@@ -207,31 +257,43 @@ public class RequestSync {


    private static void showUsage() {
    private static void showUsage() {
        System.err.println(
        System.err.println(
                "usage: requestsync [options]\n" +
                "Usage:\n" +
                "\n" +
                "  requestsync [options]\n" +
                "    With no options, a sync will be requested for all account and all sync\n" +
                "    With no options, a sync will be requested for all account and all sync\n" +
                "authorities with no extras. Options can be:\n" +
                "    authorities with no extras.\n" +
                "    Basic options:\n" +
                "       -h|--help: Display this message\n" +
                "       -h|--help: Display this message\n" +
                "       -n|--account-name <ACCOUNT-NAME>\n" +
                "       -n|--account-name <ACCOUNT-NAME>\n" +
                "       -t|--account-type <ACCOUNT-TYPE>\n" +
                "       -t|--account-type <ACCOUNT-TYPE>\n" +
                "       -a|--authority <AUTHORITY>\n" +
                "       -a|--authority <AUTHORITY>\n" +
                "  Add ContentResolver extras:\n" +
                "    ContentResolver extra options:\n" +
                "      --is|--ignore-settings: Add SYNC_EXTRAS_IGNORE_SETTINGS\n" +
                "      --is|--ignore-settings: Add SYNC_EXTRAS_IGNORE_SETTINGS\n" +
                "      --ib|--ignore-backoff: Add SYNC_EXTRAS_IGNORE_BACKOFF\n" +
                "      --ib|--ignore-backoff: Add SYNC_EXTRAS_IGNORE_BACKOFF\n" +
                "      --dd|--discard-deletions: Add SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS\n" +
                "      --dd|--discard-deletions: Add SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS\n" +
                "      --nr|--no-retry: Add SYNC_EXTRAS_DO_NOT_RETRY\n" +
                "      --nr|--no-retry: Add SYNC_EXTRAS_DO_NOT_RETRY\n" +
                "      --ex|--expedited: Add SYNC_EXTRAS_EXPEDITED\n" +
                "      --ex|--expedited: Add SYNC_EXTRAS_EXPEDITED\n" +
                "    --i|--initialize: Add SYNC_EXTRAS_INITIALIZE\n" +
                "      -i|--initialize: Add SYNC_EXTRAS_INITIALIZE\n" +
                "      --m|--manual: Add SYNC_EXTRAS_MANUAL\n" +
                "      --m|--manual: Add SYNC_EXTRAS_MANUAL\n" +
                "      --od|--override-deletions: Add SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS\n" +
                "      --od|--override-deletions: Add SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS\n" +
                "    --u|--upload-only: Add SYNC_EXTRAS_UPLOAD\n" +
                "      -u|--upload-only: Add SYNC_EXTRAS_UPLOAD\n" +
                "  Add custom extras:\n" +
                "      --rc|--require-charging: Add SYNC_EXTRAS_REQUIRE_CHARGING\n" +
                "    Custom extra options:\n" +
                "      -e|--es|--extra-string <KEY> <VALUE>\n" +
                "      -e|--es|--extra-string <KEY> <VALUE>\n" +
                "      --esn|--extra-string-null <KEY>\n" +
                "      --esn|--extra-string-null <KEY>\n" +
                "      --ei|--extra-int <KEY> <VALUE>\n" +
                "      --ei|--extra-int <KEY> <VALUE>\n" +
                "      --el|--extra-long <KEY> <VALUE>\n" +
                "      --el|--extra-long <KEY> <VALUE>\n" +
                "      --ef|--extra-float <KEY> <VALUE>\n" +
                "      --ef|--extra-float <KEY> <VALUE>\n" +
                "      --ed|--extra-double <KEY> <VALUE>\n" +
                "      --ed|--extra-double <KEY> <VALUE>\n" +
                "    --ez|--extra-bool <KEY> <VALUE>\n"
                "      --ez|--extra-bool <KEY> <VALUE>\n" +
                "\n" +
                "  requestsync add-periodic INTERVAL-SECOND [options]\n" +
                        "  requestsync remove-periodic [options]\n" +
                "    Mandatory options:\n" +
                "      -n|--account-name <ACCOUNT-NAME>\n" +
                "      -t|--account-type <ACCOUNT-TYPE>\n" +
                "      -a|--authority <AUTHORITY>\n" +
                "    Also takes the above extra options.\n"
                );
                );
    }
    }
}
}