Skip to content

Commit

Permalink
BackupAndRestore: Add restore call log function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mao Jun committed May 4, 2018
1 parent b4eb9bc commit 428dd33
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 14 deletions.
7 changes: 5 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,16 @@ dependencies {
transitive = false
}

//Gson
implementation "com.google.code.gson:gson:$rootProject.gson_version"

//dagger2
implementation "com.google.dagger:dagger:$rootProject.ext.dagger_version"
annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.ext.dagger_version"

//leakcanary
debugCompile "com.squareup.leakcanary:leakcanary-android:$rootProject.ext.leakcanary_version"
releaseCompile "com.squareup.leakcanary:leakcanary-android-no-op:$rootProject.ext.leakcanary_version"
debugImplementation "com.squareup.leakcanary:leakcanary-android:$rootProject.ext.leakcanary_version"
releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$rootProject.ext.leakcanary_version"

implementation project(':loglibrary')
implementation project(':commonlibrary')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.koma.backuprestore.data.BackupRestoreRepository;
import com.koma.backuprestore.data.entities.App;
import com.koma.loglibrary.KomaLog;
import com.koma.vcalendarlibrary.utils.LogUtil;

import java.util.List;

Expand Down Expand Up @@ -55,13 +54,12 @@ void setupListeners() {

@Override
public void subscribe() {
mRepository.restoreSms("/storage/emulated/0/sms.vmsg")
mRepository.restoreCallLog("/storage/emulated/0/call_log.json")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSubscriber<Integer>() {
@Override
public void onNext(Integer integer) {
KomaLog.d(TAG, "hahh " + integer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ public Flowable<Integer> restoreSms(String fileName) {
return mRestoreDataSource.restoreSms(fileName);
}

@Override
public Flowable<Integer> restoreCallLog(String fileName) {
return mRestoreDataSource.restoreCallLog(fileName);
}

@Override
public Flowable<Integer> restoreCalendarEvents(String fileName) {
return mRestoreDataSource.restoreCalendarEvents(fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.koma.backuprestore.data.source.backup.IBackupDataSource;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.koma.backuprestore.data.source.backup.BackupDataSource;
import com.koma.backuprestore.data.source.backup.IBackupDataSource;
import com.koma.backuprestore.data.source.restore.IRestoreDataSource;
import com.koma.backuprestore.data.source.restore.RestoreDataSource;

Expand All @@ -43,13 +45,20 @@ SharedPreferences provideSharePreferences(Context context) {

@Singleton
@Provides
IBackupDataSource provideBackupDataSource(Context context) {
return new BackupDataSource(context);
Gson provideGson() {
return new GsonBuilder()
.create();
}

@Singleton
@Provides
IBackupDataSource provideBackupDataSource(Context context, Gson gson) {
return new BackupDataSource(context, gson);
}

@Singleton
@Provides
IRestoreDataSource provideRestoreDataSource(Context context) {
return new RestoreDataSource(context);
IRestoreDataSource provideRestoreDataSource(Context context, Gson gson) {
return new RestoreDataSource(context, gson);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2017 Koma
*
* 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.koma.backuprestore.data.entities;

import com.google.gson.annotations.SerializedName;

import java.io.Serializable;

/**
* CallLogEntry
*
* @author koma_mj
* @date 5/3/18
*/

public class CallLogEntry implements Serializable {
@SerializedName("number")
public String mNumber;
@SerializedName("isRead")
public int mIsRead;
@SerializedName("name")
public String mName;
@SerializedName("type")
public int mType;
@SerializedName("date")
public long mDate;
@SerializedName("duration")
public long mDuration;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.database.Cursor;
import android.provider.MediaStore;

import com.google.gson.Gson;
import com.koma.backuprestore.data.entities.App;
import com.koma.backuprestore.data.entities.Image;
import com.koma.backuprestore.data.entities.Video;
Expand Down Expand Up @@ -58,9 +59,13 @@ public class BackupDataSource implements IBackupDataSource {

private final Context mContext;

private final Gson mGson;

@Inject
public BackupDataSource(Context context) {
public BackupDataSource(Context context, Gson gson) {
mContext = context;

mGson = gson;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ public interface IRestoreDataSource extends IBackupRestoreDataSource {

Flowable<Integer> restoreSms(String fileName);

Flowable<Integer> restoreCallLog(String fileName);

Flowable<Integer> restoreCalendarEvents(String fileName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@
package com.koma.backuprestore.data.source.restore;

import android.accounts.Account;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.provider.Telephony;

import com.google.gson.Gson;
import com.koma.backuprestore.commonlibrary.util.Constants;
import com.koma.backuprestore.data.entities.CallLogEntry;
import com.koma.backuprestore.data.entities.MmsRestoreContent;
import com.koma.backuprestore.data.entities.SmsRestoreEntry;
import com.koma.backuprestore.data.source.restore.helper.CalendarHelper;
import com.koma.backuprestore.data.source.restore.util.CallLogUtils;
import com.koma.backuprestore.data.source.restore.util.MmsUtils;
import com.koma.backuprestore.data.source.restore.util.SmsUtils;
import com.koma.loglibrary.KomaLog;
Expand All @@ -34,7 +39,6 @@
import com.koma.mmslibrary.mtkpdu.MtkPduParser;
import com.koma.mmslibrary.mtkpdu.MtkPduPersister;
import com.koma.vcalendarlibrary.VCalParser;
import com.koma.vcalendarlibrary.utils.LogUtil;
import com.koma.vcardlibrary.VCardConfig;
import com.koma.vcardlibrary.VCardEntry;
import com.koma.vcardlibrary.VCardEntryCommitter;
Expand All @@ -48,6 +52,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
Expand All @@ -68,9 +73,13 @@ public class RestoreDataSource implements IRestoreDataSource {

private final Context mContext;

private final Gson mGson;

@Inject
public RestoreDataSource(Context context) {
public RestoreDataSource(Context context, Gson gson) {
mContext = context;

mGson = gson;
}

@Override
Expand Down Expand Up @@ -230,7 +239,9 @@ public void subscribe(FlowableEmitter<Integer> emitter) {
if (cursor != null && cursor.getCount() != 0) {
KomaLog.i(TAG, "this sms is existed!");
cursor.moveToFirst();
if (cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY)).equals(contentValues.get(Telephony.Sms.BODY))) {
String body = cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY));
cursor.close();
if (body.equals(contentValues.get(Telephony.Sms.BODY))) {
emitter.onNext(++count);
continue;
}
Expand All @@ -250,6 +261,62 @@ public void subscribe(FlowableEmitter<Integer> emitter) {
}, BackpressureStrategy.LATEST);
}

@Override
public Flowable<Integer> restoreCallLog(final String fileName) {
return Flowable.create(new FlowableOnSubscribe<Integer>() {
@Override
public void subscribe(FlowableEmitter<Integer> emitter) throws Exception {
final String[] projection = new String[]{
CallLog.Calls.NUMBER, CallLog.Calls.DATE, CallLog.Calls.IS_READ,
CallLog.Calls.DURATION, CallLog.Calls.TYPE};
final ContentResolver contentResolver = mContext.getContentResolver();
int count = 0;
List<CallLogEntry> callLogEntries = new ArrayList<>();
callLogEntries.addAll(CallLogUtils.parseCallLogEntries(mGson, fileName));
for (CallLogEntry callLogEntry : callLogEntries) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(CallLog.Calls.NUMBER);
stringBuilder.append(" = ");
stringBuilder.append(callLogEntry.mNumber);
stringBuilder.append(" AND ");
stringBuilder.append(CallLog.Calls.IS_READ);
stringBuilder.append(" = ");
stringBuilder.append(callLogEntry.mIsRead);
stringBuilder.append(" AND ");
stringBuilder.append(CallLog.Calls.DATE);
stringBuilder.append(" = ");
stringBuilder.append(callLogEntry.mDate);
stringBuilder.append(" AND ");
stringBuilder.append(CallLog.Calls.DURATION);
stringBuilder.append(" = ");
stringBuilder.append(callLogEntry.mDuration);
stringBuilder.append(" AND ");
stringBuilder.append(CallLog.Calls.TYPE);
stringBuilder.append(" = ");
stringBuilder.append(callLogEntry.mType);
Cursor cursor = contentResolver.query(Constants.CALL_LOG_URI,
projection, stringBuilder.toString(), null, null);
if (cursor != null && cursor.getCount() != 0) {
cursor.close();
emitter.onNext(++count);
continue;
}
ContentValues contentValues = new ContentValues();
contentValues.put(CallLog.Calls.NUMBER, callLogEntry.mNumber);
contentValues.put(CallLog.Calls.IS_READ, callLogEntry.mIsRead);
contentValues.put(CallLog.Calls.DATE, callLogEntry.mDate);
contentValues.put(CallLog.Calls.DURATION, callLogEntry.mDuration);
contentValues.put(CallLog.Calls.TYPE, callLogEntry.mType);
contentValues.put(CallLog.Calls.CACHED_NAME, callLogEntry.mName);
if (contentResolver.insert(Constants.CALL_LOG_URI, contentValues) != null) {
emitter.onNext(++count);
}
}
emitter.onComplete();
}
}, BackpressureStrategy.LATEST);
}

@Override
public Flowable<Integer> restoreCalendarEvents(final String fileName) {
return Flowable.create(new FlowableOnSubscribe<Integer>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2017 Koma
*
* 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.koma.backuprestore.data.source.restore.util;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.koma.backuprestore.data.entities.CallLogEntry;
import com.koma.loglibrary.KomaLog;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.List;

/**
* Created by koma on 5/4/18.
*/

public class CallLogUtils {
private CallLogUtils() {
}

private static String getCallLogInfo(final String fileName) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(fileName);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = -1;
byte[] buffer = new byte[512];
while ((len = inputStream.read(buffer, 0, 512)) != -1) {
baos.write(buffer, 0, len);
}
KomaLog.e("", "" + baos.toString());
return baos.toString();
} catch (IOException | NullPointerException | IndexOutOfBoundsException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return null;
}

public static List<CallLogEntry> parseCallLogEntries(final Gson gson, final String fileName) {
Type listType = new TypeToken<List<CallLogEntry>>() {
}.getType();
return gson.fromJson(getCallLogInfo(fileName), listType);
}
}
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ ext {
rxandroid_version = '2.0.1'
butterknife_version = '8.8.1'
room_version = '1.0.0'
gson_version = '2.8.0'
eventbus_version = '3.1.1'
leakcanary_version = '1.5.4'

Expand Down

0 comments on commit 428dd33

Please sign in to comment.