If you think the Android project sms-backup-plus listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/* Copyright (c) 2009 Christoph Studer <chstuder@gmail.com>
* Copyright (c) 2010 Jan Berkel <jan.berkel@gmail.com>
*/*www.java2s.com*/
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package com.zegoggles.smssync.service;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.zegoggles.smssync.preferences.Preferences;
import java.util.UUID;
importstatic com.zegoggles.smssync.App.LOCAL_LOGV;
importstatic com.zegoggles.smssync.App.TAG;
importstatic com.zegoggles.smssync.service.BackupType.*;
publicclass Alarms {
privatestaticfinalint BOOT_BACKUP_DELAY = 60;
privatefinal Preferences mPreferences;
private Context mContext;
public Alarms(Context context) {
this(context.getApplicationContext(), new Preferences(context));
}
Alarms(Context context, Preferences preferences) {
mContext = context.getApplicationContext();
mPreferences = preferences;
}
publiclong scheduleIncomingBackup() {
return scheduleBackup(mPreferences.getIncomingTimeoutSecs(), INCOMING, false);
}
publiclong scheduleRegularBackup() {
return scheduleBackup(mPreferences.getRegularTimeoutSecs(), REGULAR, false);
}
publiclong scheduleBootupBackup() {
return scheduleBackup(BOOT_BACKUP_DELAY, REGULAR, false);
}
publiclong scheduleImmediateBackup() {
return scheduleBackup(-1, BROADCAST_INTENT, true);
}
publicvoid cancel() {
getAlarmManager(mContext).cancel(createPendingIntent(mContext, UNKNOWN));
}
privatelong scheduleBackup(int inSeconds, BackupType backupType, boolean force) {
if (LOCAL_LOGV) {
Log.v(TAG, "scheduleBackup(" + mContext + ", " + inSeconds + ", " + backupType + ", " + force + ")");
}
if (force || (mPreferences.isEnableAutoSync() && inSeconds > 0)) {
finallong atTime = System.currentTimeMillis() + (inSeconds * 1000l);
getAlarmManager(mContext).set(AlarmManager.RTC_WAKEUP, atTime, createPendingIntent(mContext, backupType));
if (LOCAL_LOGV) {
Log.v(TAG, "Scheduled backup due " + (inSeconds > 0 ? "in " + inSeconds + " seconds" : "now"));
}
return atTime;
} else {
if (LOCAL_LOGV) Log.v(TAG, "Not scheduling backup because auto sync is disabled.");
return -1;
}
}
privatestatic AlarmManager getAlarmManager(Context ctx) {
return (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
}
privatestatic PendingIntent createPendingIntent(Context ctx, BackupType backupType) {
final UUID uuid = UUID.randomUUID();
final Intent intent = (new Intent(ctx, SmsBackupService.class))
.setAction(backupType.name() + "-" + uuid.toString()) // create fresh pending intent
.putExtra(BackupType.EXTRA, backupType.name());
return PendingIntent.getService(ctx, 0, intent, 0);
}
}