• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 **
3 ** Copyright 2012, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 package com.android.commands.requestsync;
19 
20 import android.accounts.Account;
21 import android.content.ContentResolver;
22 import android.os.Bundle;
23 
24 import java.net.URISyntaxException;
25 
26 public class RequestSync {
27     // agr parsing fields
28     private String[] mArgs;
29     private int mNextArg;
30     private String mCurArgData;
31 
32     // account & authority
33     private String mAccountName = null;
34     private String mAccountType = null;
35     private String mAuthority = null;
36 
37     // extras
38     private Bundle mExtras = new Bundle();
39 
40     /**
41      * Command-line entry point.
42      *
43      * @param args The command-line arguments
44      */
main(String[] args)45     public static void main(String[] args) {
46         try {
47             (new RequestSync()).run(args);
48         } catch (IllegalArgumentException e) {
49             showUsage();
50             System.err.println("Error: " + e);
51             e.printStackTrace();
52         } catch (Exception e) {
53             e.printStackTrace(System.err);
54             System.exit(1);
55         }
56     }
57 
run(String[] args)58     private void run(String[] args) throws Exception {
59         mArgs = args;
60         mNextArg = 0;
61 
62         final boolean ok = parseArgs();
63         if (ok) {
64             final Account account = mAccountName != null && mAccountType != null
65                     ? new Account(mAccountName, mAccountType) : null;
66 
67             System.out.printf("Requesting sync for: \n");
68             if (account != null) {
69                 System.out.printf("  Account: %s (%s)\n", account.name, account.type);
70             } else {
71                 System.out.printf("  Account: all\n");
72             }
73 
74             System.out.printf("  Authority: %s\n", mAuthority != null ? mAuthority : "All");
75 
76             if (mExtras.size() > 0) {
77                 System.out.printf("  Extras:\n");
78                 for (String key : mExtras.keySet()) {
79                     System.out.printf("    %s: %s\n", key, mExtras.get(key));
80                 }
81             }
82 
83             ContentResolver.requestSync(account, mAuthority, mExtras);
84         }
85     }
86 
parseArgs()87     private boolean parseArgs() throws URISyntaxException {
88         String opt;
89         while ((opt=nextOption()) != null) {
90             if (opt.equals("-h") || opt.equals("--help")) {
91                 showUsage();
92                 return false;
93             } else if (opt.equals("-n") || opt.equals("--account-name")) {
94                 mAccountName = nextArgRequired();
95             } else if (opt.equals("-t") || opt.equals("--account-type")) {
96                 mAccountType = nextArgRequired();
97             } else if (opt.equals("-a") || opt.equals("--authority")) {
98                 mAuthority = nextArgRequired();
99             } else if (opt.equals("--is") || opt.equals("--ignore-settings")) {
100                 mExtras.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, true);
101             } else if (opt.equals("--ib") || opt.equals("--ignore-backoff")) {
102                 mExtras.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, true);
103             } else if (opt.equals("--dd") || opt.equals("--discard-deletions")) {
104                 mExtras.putBoolean(ContentResolver.SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS, true);
105             } else if (opt.equals("--nr") || opt.equals("--no-retry")) {
106                 mExtras.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, true);
107             } else if (opt.equals("--ex") || opt.equals("--expedited")) {
108                 mExtras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
109             } else if (opt.equals("-i") || opt.equals("--initialize")) {
110                 mExtras.putBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, true);
111             } else if (opt.equals("-m") || opt.equals("--manual")) {
112                 mExtras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
113             } else if (opt.equals("--od") || opt.equals("--override-deletions")) {
114                 mExtras.putBoolean(ContentResolver.SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS, true);
115             } else if (opt.equals("-u") || opt.equals("--upload-only")) {
116                 mExtras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, true);
117             } else if (opt.equals("-e") || opt.equals("--es") || opt.equals("--extra-string")) {
118                 final String key = nextArgRequired();
119                 final String value = nextArgRequired();
120                 mExtras.putString(key, value);
121             } else if (opt.equals("--esn") || opt.equals("--extra-string-null")) {
122                 final String key = nextArgRequired();
123                 mExtras.putString(key, null);
124             } else if (opt.equals("--ei") || opt.equals("--extra-int")) {
125                 final String key = nextArgRequired();
126                 final String value = nextArgRequired();
127                 mExtras.putInt(key, Integer.valueOf(value));
128             } else if (opt.equals("--el") || opt.equals("--extra-long")) {
129                 final String key = nextArgRequired();
130                 final String value = nextArgRequired();
131                 mExtras.putLong(key, Long.parseLong(value));
132             } else if (opt.equals("--ef") || opt.equals("--extra-float")) {
133                 final String key = nextArgRequired();
134                 final String value = nextArgRequired();
135                 mExtras.putFloat(key, Long.parseLong(value));
136             } else if (opt.equals("--ed") || opt.equals("--extra-double")) {
137                 final String key = nextArgRequired();
138                 final String value = nextArgRequired();
139                 mExtras.putFloat(key, Long.parseLong(value));
140             } else if (opt.equals("--ez") || opt.equals("--extra-bool")) {
141                 final String key = nextArgRequired();
142                 final String value = nextArgRequired();
143                 mExtras.putBoolean(key, Boolean.valueOf(value));
144             } else {
145                 System.err.println("Error: Unknown option: " + opt);
146                 showUsage();
147                 return false;
148             }
149         }
150 
151         if (mNextArg < mArgs.length) {
152             showUsage();
153             return false;
154         }
155         return true;
156     }
157 
nextOption()158     private String nextOption() {
159         if (mCurArgData != null) {
160             String prev = mArgs[mNextArg - 1];
161             throw new IllegalArgumentException("No argument expected after \"" + prev + "\"");
162         }
163         if (mNextArg >= mArgs.length) {
164             return null;
165         }
166         String arg = mArgs[mNextArg];
167         if (!arg.startsWith("-")) {
168             return null;
169         }
170         mNextArg++;
171         if (arg.equals("--")) {
172             return null;
173         }
174         if (arg.length() > 1 && arg.charAt(1) != '-') {
175             if (arg.length() > 2) {
176                 mCurArgData = arg.substring(2);
177                 return arg.substring(0, 2);
178             } else {
179                 mCurArgData = null;
180                 return arg;
181             }
182         }
183         mCurArgData = null;
184         return arg;
185     }
186 
nextArg()187     private String nextArg() {
188         if (mCurArgData != null) {
189             String arg = mCurArgData;
190             mCurArgData = null;
191             return arg;
192         } else if (mNextArg < mArgs.length) {
193             return mArgs[mNextArg++];
194         } else {
195             return null;
196         }
197     }
198 
nextArgRequired()199     private String nextArgRequired() {
200         String arg = nextArg();
201         if (arg == null) {
202             String prev = mArgs[mNextArg - 1];
203             throw new IllegalArgumentException("Argument expected after \"" + prev + "\"");
204         }
205         return arg;
206     }
207 
showUsage()208     private static void showUsage() {
209         System.err.println(
210                 "usage: requestsync [options]\n" +
211                 "With no options, a sync will be requested for all account and all sync\n" +
212                 "authorities with no extras. Options can be:\n" +
213                 "    -h|--help: Display this message\n" +
214                 "    -n|--account-name <ACCOUNT-NAME>\n" +
215                 "    -t|--account-type <ACCOUNT-TYPE>\n" +
216                 "    -a|--authority <AUTHORITY>\n" +
217                 "  Add ContentResolver extras:\n" +
218                 "    --is|--ignore-settings: Add SYNC_EXTRAS_IGNORE_SETTINGS\n" +
219                 "    --ib|--ignore-backoff: Add SYNC_EXTRAS_IGNORE_BACKOFF\n" +
220                 "    --dd|--discard-deletions: Add SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS\n" +
221                 "    --nr|--no-retry: Add SYNC_EXTRAS_DO_NOT_RETRY\n" +
222                 "    --ex|--expedited: Add SYNC_EXTRAS_EXPEDITED\n" +
223                 "    --i|--initialize: Add SYNC_EXTRAS_INITIALIZE\n" +
224                 "    --m|--manual: Add SYNC_EXTRAS_MANUAL\n" +
225                 "    --od|--override-deletions: Add SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS\n" +
226                 "    --u|--upload-only: Add SYNC_EXTRAS_UPLOAD\n" +
227                 "  Add custom extras:\n" +
228                 "    -e|--es|--extra-string <KEY> <VALUE>\n" +
229                 "    --esn|--extra-string-null <KEY>\n" +
230                 "    --ei|--extra-int <KEY> <VALUE>\n" +
231                 "    --el|--extra-long <KEY> <VALUE>\n" +
232                 "    --ef|--extra-float <KEY> <VALUE>\n" +
233                 "    --ed|--extra-double <KEY> <VALUE>\n" +
234                 "    --ez|--extra-bool <KEY> <VALUE>\n"
235                 );
236     }
237 }
238