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
package com.zegoggles.smssync.service;
//fromwww.java2s.comimport android.content.Context;
import android.net.Uri;
import android.provider.CallLog;
import android.text.TextUtils;
import android.util.Log;
import com.zegoggles.smssync.Consts;
import com.zegoggles.smssync.MmsConsts;
import com.zegoggles.smssync.SmsConsts;
import com.zegoggles.smssync.contacts.ContactGroupIds;
import com.zegoggles.smssync.mail.DataType;
import org.jetbrains.annotations.Nullable;
import java.util.Locale;
import java.util.Set;
importstatic com.zegoggles.smssync.App.LOCAL_LOGV;
importstatic com.zegoggles.smssync.App.TAG;
importstatic com.zegoggles.smssync.mail.DataType.*;
class BackupQueryBuilder {
privatefinal Context context;
// only query for needed fields
// http://stackoverflow.com/questions/12033234/get-calls-provider-internal-structure
privatestaticfinal String[] CALLLOG_PROJECTION = {
CallLog.Calls._ID,
CallLog.Calls.NUMBER,
CallLog.Calls.DURATION,
CallLog.Calls.DATE,
CallLog.Calls.TYPE
};
public BackupQueryBuilder(Context context) {
this.context = context;
}
staticclass Query {
final Uri uri;
final String[] projection;
final String selection;
final String[] selectionArgs;
final String sortOrder;
Query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
this.uri = uri;
this.projection = projection;
this.selection = selection;
this.selectionArgs = selectionArgs;
this.sortOrder = sortOrder;
}
Query(Uri uri, String[] projection, String selection, String[] selectionArgs, int max) {
this(uri, projection, selection, selectionArgs,
max > 0 ? SmsConsts.DATE + " LIMIT "+max : SmsConsts.DATE);
}
}
public @Nullable Query buildQueryForDataType(DataType type, @Nullable ContactGroupIds groupIds, int max) {
switch (type) {
case SMS: return getQueryForSMS(groupIds, max);
case MMS: return getQueryForMMS(groupIds, max);
case CALLLOG: return getQueryForCallLog(max);
default: return null;
}
}
public @Nullable Query buildMostRecentQueryForDataType(DataType type) {
switch (type) {
case MMS:
returnnew Query(
Consts.MMS_PROVIDER,
new String[] {MmsConsts.DATE },
null,
null,
MmsConsts.DATE + " DESC LIMIT 1");
case SMS:
returnnew Query(
Consts.SMS_PROVIDER,
new String[]{SmsConsts.DATE},
SmsConsts.TYPE + " <> ?",
new String[]{String.valueOf(SmsConsts.MESSAGE_TYPE_DRAFT)},
SmsConsts.DATE + " DESC LIMIT 1");
case CALLLOG:
returnnew Query(
Consts.CALLLOG_PROVIDER,
new String[]{CallLog.Calls.DATE},
null,
null,
CallLog.Calls.DATE + " DESC LIMIT 1");
default:
return null;
}
}
private Query getQueryForSMS(@Nullable ContactGroupIds groupIds, int max) {
returnnew Query(Consts.SMS_PROVIDER,
null,
String.format(Locale.ENGLISH,
"%s > ? AND %s <> ? %s",
SmsConsts.DATE,
SmsConsts.TYPE,
groupSelection(SMS, groupIds)).trim(),
new String[] {
String.valueOf(SMS.getMaxSyncedDate(context)),
String.valueOf(SmsConsts.MESSAGE_TYPE_DRAFT)
},
max);
}
private Query getQueryForMMS(@Nullable ContactGroupIds group, int max) {
long maxSynced = MMS.getMaxSyncedDate(context);
if (maxSynced > 0) {
// NB: max synced date is stored in seconds since epoch in database
maxSynced = (long) (maxSynced / 1000d);
}
returnnew Query(
Consts.MMS_PROVIDER,
null,
String.format(Locale.ENGLISH, "%s > ? AND %s <> ? %s",
SmsConsts.DATE,
MmsConsts.TYPE,
groupSelection(DataType.MMS, group)).trim(),
new String[] {
String.valueOf(maxSynced),
MmsConsts.DELIVERY_REPORT
},
max);
}
private Query getQueryForCallLog(int max) {
returnnew Query(
Consts.CALLLOG_PROVIDER,
CALLLOG_PROJECTION,
String.format(Locale.ENGLISH, "%s > ?", CallLog.Calls.DATE),
new String[] {
String.valueOf(CALLLOG.getMaxSyncedDate(context))
},
max);
}
private String groupSelection(DataType type, @Nullable ContactGroupIds group) {
/* Only MMS selection is supported at the moment */if (type != SMS || group == null) {
return"";
}
final Set<Long> ids = group.getRawIds();
if (LOCAL_LOGV) Log.v(TAG, "only selecting contacts matching " + ids);
return String.format(Locale.ENGLISH, " AND (%s = %d OR %s IN (%s))",
SmsConsts.TYPE,
SmsConsts.MESSAGE_TYPE_SENT,
SmsConsts.PERSON,
TextUtils.join(",", ids.toArray(new Long[ids.size()])));
}
}