Permalink
Browse files

Added api level check for periodic sync code, so the native addPeriod…

…icSync will be called for api-8+.
  • Loading branch information...
1 parent b8658fb commit af24162a46696441304a650e22ca859df56e3c65 @maxpower47 committed Jan 5, 2011
Showing with 35 additions and 14 deletions.
  1. +1 −1 AndroidManifest.xml
  2. +1 −1 default.properties
  3. +33 −12 src/com/pindroid/util/SyncUtils.java
View
@@ -26,7 +26,7 @@
package="com.pindroid"
android:versionCode="5"
android:versionName="1.1.1">
- <uses-sdk android:minSdkVersion="5" />
+ <uses-sdk android:minSdkVersion="5" android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
View
@@ -8,4 +8,4 @@
# project structure.
# Project target.
-target=android-5
+target=Google Inc.:Google APIs:8
@@ -21,10 +21,14 @@
package com.pindroid.util;
+import com.pindroid.Constants;
import com.pindroid.syncadapter.PeriodicSyncReceiver;
+import android.accounts.Account;
+import android.accounts.AccountManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
+import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;
import android.os.SystemClock;
@@ -33,21 +37,38 @@
public static void addPeriodicSync(String authority, Bundle extras, long frequency, Context context) {
long pollFrequencyMsec = frequency * 60000;
- AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
-
- int type = AlarmManager.ELAPSED_REALTIME_WAKEUP;
- long triggerAtTime = SystemClock.elapsedRealtime() + pollFrequencyMsec;
- long interval = pollFrequencyMsec;
- PendingIntent operation = PeriodicSyncReceiver.createPendingIntent(context, authority, extras);
-
- manager.setInexactRepeating(type, triggerAtTime, interval, operation);
-
+
+ if(android.os.Build.VERSION.SDK_INT < 8) {
+ AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
+
+ int type = AlarmManager.ELAPSED_REALTIME_WAKEUP;
+ long triggerAtTime = SystemClock.elapsedRealtime() + pollFrequencyMsec;
+ long interval = pollFrequencyMsec;
+ PendingIntent operation = PeriodicSyncReceiver.createPendingIntent(context, authority, extras);
+
+ manager.setInexactRepeating(type, triggerAtTime, interval, operation);
+ } else {
+ AccountManager am = AccountManager.get(context);
+ Account[] accounts = am.getAccountsByType(Constants.ACCOUNT_TYPE);
+
+ for(Account a : accounts) {
+ ContentResolver.addPeriodicSync(a, authority, extras, frequency * 60);
+ }
+ }
}
- public static void removePeriodicSync(String authority, Bundle extras, Context context) {
+ public static void removePeriodicSync(String authority, Bundle extras, Context context) {
+ if(android.os.Build.VERSION.SDK_INT >= 8) {
+ AccountManager am = AccountManager.get(context);
+ Account[] accounts = am.getAccountsByType(Constants.ACCOUNT_TYPE);
+
+ for(Account a : accounts) {
+ ContentResolver.removePeriodicSync(a, authority, extras);
+ }
+ }
+
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent operation = PeriodicSyncReceiver.createPendingIntent(context, authority, extras);
manager.cancel(operation);
}
-
-}
+}

0 comments on commit af24162

Please sign in to comment.