Merge branch 'main' of https://bdgit.educoder.net/pnv9folh3/18myx into 18myx_branch
commit
77a118c177
After Width: | Height: | Size: 79 KiB |
Binary file not shown.
After Width: | Height: | Size: 79 KiB |
@ -0,0 +1,83 @@
|
|||||||
|
`/*
|
||||||
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
||||||
|
*
|
||||||
|
* 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 net.micode.notes.data;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.provider.ContactsContract.CommonDataKinds.Phone;
|
||||||
|
import android.provider.ContactsContract.Data;
|
||||||
|
import android.telephony.PhoneNumberUtils;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
//change
|
||||||
|
public class Contact {//联系人
|
||||||
|
private static HashMap<String, String> sContactCache;
|
||||||
|
private static final String TAG = "Contact";
|
||||||
|
|
||||||
|
//定义字符串CALLER_ID_SELECTION
|
||||||
|
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER
|
||||||
|
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"
|
||||||
|
+ " AND " + Data.RAW_CONTACT_ID + " IN "
|
||||||
|
+ "(SELECT raw_contact_id "
|
||||||
|
+ " FROM phone_lookup"
|
||||||
|
+ " WHERE min_match = '+')";
|
||||||
|
|
||||||
|
//获取联系人
|
||||||
|
public static String getContact(Context context, String phoneNumber) {
|
||||||
|
if(sContactCache == null) {
|
||||||
|
sContactCache = new HashMap<String, String>();
|
||||||
|
}
|
||||||
|
|
||||||
|
//查找HashMap中是否有phoneNumeber信息
|
||||||
|
if(sContactCache.containsKey(phoneNumber)) {
|
||||||
|
return sContactCache.get(phoneNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
String selection = CALLER_ID_SELECTION.replace("+",
|
||||||
|
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
|
||||||
|
//查找数据库中phoneNumber信息
|
||||||
|
Cursor cursor = context.getContentResolver().query(
|
||||||
|
Data.CONTENT_URI,
|
||||||
|
new String [] { Phone.DISPLAY_NAME },
|
||||||
|
selection,
|
||||||
|
new String[] { phoneNumber },
|
||||||
|
null);
|
||||||
|
|
||||||
|
//判定查询结果
|
||||||
|
//moveToFirst()返回第一条
|
||||||
|
|
||||||
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
|
try {
|
||||||
|
//找到关键信息
|
||||||
|
String name =cursor.getString(0);
|
||||||
|
sContactCache.put(phoneNumber, name);
|
||||||
|
return name;
|
||||||
|
//异常
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
Log.e(TAG, " Cursor get string error " + e.toString());
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
cursor.close();
|
||||||
|
}
|
||||||
|
//未找到相关信息
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "No contact matched with number:" + phoneNumber);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,140 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
||||||
|
*
|
||||||
|
* 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 net.micode.notes.widget;
|
||||||
|
import android.app.PendingIntent;
|
||||||
|
import android.appwidget.AppWidgetManager;
|
||||||
|
import android.appwidget.AppWidgetProvider;
|
||||||
|
import android.content.ContentValues;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.widget.RemoteViews;
|
||||||
|
|
||||||
|
import net.micode.notes.R;
|
||||||
|
import net.micode.notes.data.Notes;
|
||||||
|
import net.micode.notes.data.Notes.NoteColumns;
|
||||||
|
import net.micode.notes.tool.ResourceParser;
|
||||||
|
import net.micode.notes.ui.NoteEditActivity;
|
||||||
|
import net.micode.notes.ui.NotesListActivity;
|
||||||
|
|
||||||
|
public abstract class NoteWidgetProvider extends AppWidgetProvider {
|
||||||
|
// 定义要从数据库中查询的列
|
||||||
|
public static final String [] PROJECTION = new String [] {
|
||||||
|
NoteColumns.ID,
|
||||||
|
NoteColumns.BG_COLOR_ID,
|
||||||
|
NoteColumns.SNIPPET
|
||||||
|
};
|
||||||
|
|
||||||
|
public static final int COLUMN_ID = 0;// 笔记ID
|
||||||
|
public static final int COLUMN_BG_COLOR_ID = 1;// 背景颜色ID
|
||||||
|
public static final int COLUMN_SNIPPET = 2;// 摘要
|
||||||
|
|
||||||
|
private static final String TAG = "NoteWidgetProvider";// 日志标签
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDeleted(Context context, int[] appWidgetIds) {
|
||||||
|
ContentValues values = new ContentValues();
|
||||||
|
values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
|
||||||
|
for (int i = 0; i < appWidgetIds.length; i++) {
|
||||||
|
context.getContentResolver().update(Notes.CONTENT_NOTE_URI,
|
||||||
|
values,
|
||||||
|
NoteColumns.WIDGET_ID + "=?",
|
||||||
|
new String[] { String.valueOf(appWidgetIds[i])});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//取消与任何特定笔记的关联
|
||||||
|
private Cursor getNoteWidgetInfo(Context context, int widgetId) {
|
||||||
|
return context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
|
||||||
|
PROJECTION,
|
||||||
|
NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?",
|
||||||
|
new String[] { String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLER) },
|
||||||
|
null);
|
||||||
|
}
|
||||||
|
//检索有关笔记小部件的信息
|
||||||
|
protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||||
|
update(context, appWidgetManager, appWidgetIds, false);
|
||||||
|
}
|
||||||
|
//更新小部件
|
||||||
|
private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds,
|
||||||
|
boolean privacyMode) {
|
||||||
|
for (int i = 0; i < appWidgetIds.length; i++) {
|
||||||
|
if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) {
|
||||||
|
// 获取默认背景 ID
|
||||||
|
int bgId = ResourceParser.getDefaultBgId(context);
|
||||||
|
String snippet = "";
|
||||||
|
Intent intent = new Intent(context, NoteEditActivity.class);
|
||||||
|
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||||
|
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]);
|
||||||
|
intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType());
|
||||||
|
// 获取与小部件 ID 相关的笔记信息
|
||||||
|
Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]);
|
||||||
|
if (c != null && c.moveToFirst()) {
|
||||||
|
if (c.getCount() > 1) {
|
||||||
|
// 如果存在多个具有相同小部件 ID 的消息,记录错误并返回
|
||||||
|
Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]);
|
||||||
|
c.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
snippet = c.getString(COLUMN_SNIPPET);
|
||||||
|
bgId = c.getInt(COLUMN_BG_COLOR_ID);
|
||||||
|
intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID));
|
||||||
|
intent.setAction(Intent.ACTION_VIEW);
|
||||||
|
} else {
|
||||||
|
// 如果没有相关笔记信息,设置默认的小部件内容
|
||||||
|
snippet = context.getResources().getString(R.string.widget_havenot_content);
|
||||||
|
intent.setAction(Intent.ACTION_INSERT_OR_EDIT);
|
||||||
|
}
|
||||||
|
// 关闭游标
|
||||||
|
if (c != null) {
|
||||||
|
c.close();
|
||||||
|
}
|
||||||
|
RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());
|
||||||
|
rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
|
||||||
|
intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);
|
||||||
|
/**
|
||||||
|
* Generate the pending intent to start host for the widget
|
||||||
|
*/
|
||||||
|
// 创建用于启动宿主应用的待定意图
|
||||||
|
PendingIntent pendingIntent = null;
|
||||||
|
if (privacyMode) {
|
||||||
|
// 如果处于隐私模式,设置小部件文本为相应的提示
|
||||||
|
rv.setTextViewText(R.id.widget_text,
|
||||||
|
context.getString(R.string.widget_under_visit_mode));
|
||||||
|
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent(
|
||||||
|
context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
} else {
|
||||||
|
// 否则,设置小部件文本为笔记片段,并关联相应的意图
|
||||||
|
rv.setTextViewText(R.id.widget_text, snippet);
|
||||||
|
pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent,
|
||||||
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
}
|
||||||
|
// 设置小部件文本的点击事件
|
||||||
|
rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
|
||||||
|
// 更新小部件
|
||||||
|
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract int getBgResourceId(int bgId);
|
||||||
|
|
||||||
|
protected abstract int getLayoutId();
|
||||||
|
|
||||||
|
protected abstract int getWidgetType();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
||||||
|
*
|
||||||
|
* 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 net.micode.notes.widget;
|
||||||
|
|
||||||
|
import android.appwidget.AppWidgetManager;
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import net.micode.notes.R;
|
||||||
|
import net.micode.notes.data.Notes;
|
||||||
|
import net.micode.notes.tool.ResourceParser;
|
||||||
|
|
||||||
|
|
||||||
|
public class NoteWidgetProvider_2x extends NoteWidgetProvider {
|
||||||
|
//扩展了基本的 NoteWidgetProvider专门处理 2x 大小的小部件
|
||||||
|
@Override
|
||||||
|
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||||
|
//调用超类的 update 方法来处理小部件的更新
|
||||||
|
super.update(context, appWidgetManager, appWidgetIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getLayoutId() {
|
||||||
|
//返回 2x 小部件的布局资源 ID
|
||||||
|
return R.layout.widget_2x;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getBgResourceId(int bgId) {
|
||||||
|
//根据提供的 bgId 映射小部件的背景资源 ID
|
||||||
|
return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId);
|
||||||
|
//检索适当的资源
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getWidgetType() {
|
||||||
|
//将小部件类型指定为 Notes.TYPE_WIDGET_2X
|
||||||
|
return Notes.TYPE_WIDGET_2X;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
||||||
|
*
|
||||||
|
* 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 net.micode.notes.widget;
|
||||||
|
|
||||||
|
import android.appwidget.AppWidgetManager;
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import net.micode.notes.R;
|
||||||
|
import net.micode.notes.data.Notes;
|
||||||
|
import net.micode.notes.tool.ResourceParser;
|
||||||
|
|
||||||
|
|
||||||
|
public class NoteWidgetProvider_4x extends NoteWidgetProvider {
|
||||||
|
//扩展了基本的 NoteWidgetProvider专门处理 4x 大小的小部件
|
||||||
|
@Override
|
||||||
|
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||||
|
//小部件的 onUpdate 回调的重写
|
||||||
|
super.update(context, appWidgetManager, appWidgetIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int getLayoutId() {
|
||||||
|
//返回 4x 小部件的布局资源 ID
|
||||||
|
return R.layout.widget_4x;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getBgResourceId(int bgId) {
|
||||||
|
//映射小部件的背景资源 ID
|
||||||
|
return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getWidgetType() {
|
||||||
|
return Notes.TYPE_WIDGET_4X;
|
||||||
|
//将小部件类型指定为 Notes.TYPE_WIDGET_4X
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//定义了 4x 大小笔记小部件的行为和外观
|
@ -0,0 +1,251 @@
|
|||||||
|
package net.micode.notes.tool;
|
||||||
|
|
||||||
|
// 导入必要的类
|
||||||
|
import android.content.Context;
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.os.Environment;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.text.format.DateFormat;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintStream;
|
||||||
|
|
||||||
|
// 备份工具类
|
||||||
|
public class BackupUtils {
|
||||||
|
private static final String TAG = "BackupUtils";
|
||||||
|
|
||||||
|
// 单例实例
|
||||||
|
private static BackupUtils sInstance;
|
||||||
|
|
||||||
|
// 获取实例的静态方法,确保在整个应用程序中只有一个实例存在
|
||||||
|
public static synchronized BackupUtils getInstance(Context context) {
|
||||||
|
if (sInstance == null) {
|
||||||
|
sInstance = new BackupUtils(context);
|
||||||
|
}
|
||||||
|
return sInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 数据导出类
|
||||||
|
private TextExport mTextExport;
|
||||||
|
|
||||||
|
// 私有构造函数
|
||||||
|
private BackupUtils(Context context) {
|
||||||
|
mTextExport = new TextExport(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查外部存储是否可用
|
||||||
|
private static boolean externalStorageAvailable() {
|
||||||
|
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 导出数据到文本文件
|
||||||
|
public int exportToText() {
|
||||||
|
return mTextExport.exportToText();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取导出的文本文件名
|
||||||
|
public String getExportedTextFileName() {
|
||||||
|
return mTextExport.mFileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取导出的文本文件目录
|
||||||
|
public String getExportedTextFileDir() {
|
||||||
|
return mTextExport.mFileDirectory;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 内部私有类,负责文本导出功能
|
||||||
|
private static class TextExport {
|
||||||
|
// 笔记和数据表的投影
|
||||||
|
private static final String[] NOTE_PROJECTION = {
|
||||||
|
NoteColumns.ID,
|
||||||
|
NoteColumns.MODIFIED_DATE,
|
||||||
|
NoteColumns.SNIPPET,
|
||||||
|
NoteColumns.TYPE
|
||||||
|
};
|
||||||
|
|
||||||
|
private static final String[] DATA_PROJECTION = {
|
||||||
|
DataColumns.CONTENT,
|
||||||
|
DataColumns.MIME_TYPE,
|
||||||
|
DataColumns.DATA1,
|
||||||
|
DataColumns.DATA2,
|
||||||
|
DataColumns.DATA3,
|
||||||
|
DataColumns.DATA4,
|
||||||
|
};
|
||||||
|
|
||||||
|
// 笔记和数据表的列索引
|
||||||
|
private static final int NOTE_COLUMN_ID = 0;
|
||||||
|
private static final int NOTE_COLUMN_MODIFIED_DATE = 1;
|
||||||
|
private static final int NOTE_COLUMN_SNIPPET = 2;
|
||||||
|
private static final int DATA_COLUMN_CONTENT = 0;
|
||||||
|
private static final int DATA_COLUMN_MIME_TYPE = 1;
|
||||||
|
private static final int DATA_COLUMN_CALL_DATE = 2;
|
||||||
|
private static final int DATA_COLUMN_PHONE_NUMBER = 4;
|
||||||
|
|
||||||
|
// 文本格式数组
|
||||||
|
private final String[] TEXT_FORMAT;
|
||||||
|
private static final int FORMAT_FOLDER_NAME = 0;
|
||||||
|
private static final int FORMAT_NOTE_DATE = 1;
|
||||||
|
private static final int FORMAT_NOTE_CONTENT = 2;
|
||||||
|
|
||||||
|
// 上下文和文件名变量
|
||||||
|
private Context mContext;
|
||||||
|
private String mFileName;
|
||||||
|
private String mFileDirectory;
|
||||||
|
|
||||||
|
// 构造函数
|
||||||
|
public TextExport(Context context) {
|
||||||
|
TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note);
|
||||||
|
mContext = context;
|
||||||
|
mFileName = "";
|
||||||
|
mFileDirectory = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取文本格式字符串
|
||||||
|
private String getFormat(int id) {
|
||||||
|
return TEXT_FORMAT[id];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 导出文件夹到文本
|
||||||
|
private void exportFolderToText(String folderId, PrintStream ps) {
|
||||||
|
// 查询文件夹下的笔记
|
||||||
|
Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI,
|
||||||
|
NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] {
|
||||||
|
folderId
|
||||||
|
}, null);
|
||||||
|
|
||||||
|
// 如果存在笔记,则导出
|
||||||
|
if (notesCursor != null) {
|
||||||
|
if (notesCursor.moveToFirst()) {
|
||||||
|
do {
|
||||||
|
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
|
||||||
|
mContext.getString(R.string.format_datetime_mdhm),
|
||||||
|
notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))));
|
||||||
|
String noteId = notesCursor.getString(NOTE_COLUMN_ID);
|
||||||
|
exportNoteToText(noteId, ps);
|
||||||
|
} while (notesCursor.moveToNext());
|
||||||
|
}
|
||||||
|
notesCursor.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 导出笔记到文本
|
||||||
|
private void exportNoteToText(String noteId, PrintStream ps) {
|
||||||
|
Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI,
|
||||||
|
DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] {
|
||||||
|
noteId
|
||||||
|
}, null);
|
||||||
|
|
||||||
|
if (dataCursor != null) {
|
||||||
|
if (dataCursor.moveToFirst()) {
|
||||||
|
do {
|
||||||
|
// 导出笔记内容
|
||||||
|
String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE);
|
||||||
|
if (DataConstants.CALL_NOTE.equals(mimeType)) {
|
||||||
|
// 导出电话笔记内容
|
||||||
|
// ...
|
||||||
|
} else if (DataConstants.NOTE.equals(mimeType)) {
|
||||||
|
// 导出普通笔记内容
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
} while (dataCursor.moveToNext());
|
||||||
|
}
|
||||||
|
dataCursor.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 导出到文本文件的主方法
|
||||||
|
public int exportToText() {
|
||||||
|
// 检查外部存储是否可用
|
||||||
|
if (!externalStorageAvailable()) {
|
||||||
|
Log.d(TAG, "Media was not mounted");
|
||||||
|
return STATE_SD_CARD_UNMOUONTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取文本文件打印流
|
||||||
|
PrintStream ps = getExportToTextPrintStream();
|
||||||
|
if (ps == null) {
|
||||||
|
Log.e(TAG, "get print stream error");
|
||||||
|
return STATE_SYSTEM_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 导出文件夹和笔记到文本文件
|
||||||
|
// ...
|
||||||
|
|
||||||
|
return STATE_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取导出到文本文件的打印流
|
||||||
|
private PrintStream getExportToTextPrintStream() {
|
||||||
|
// 生成文件
|
||||||
|
File file = generateFileMountedOnSDcard(mContext, R.string.file_path,
|
||||||
|
R.string.file_name_txt_format);
|
||||||
|
if (file == null) {
|
||||||
|
Log.e(TAG, "create file to exported failed");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
mFileName = file.getName();
|
||||||
|
mFileDirectory = mContext.getString(R.string.file_path);
|
||||||
|
PrintStream ps = null;
|
||||||
|
try {
|
||||||
|
FileOutputStream fos = new FileOutputStream(file);
|
||||||
|
ps = new PrintStream(fos);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
} catch (NullPointerException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return ps;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 导出状态常量
|
||||||
|
public static final int STATE_SD_CARD_UNMOUONTED = 0;
|
||||||
|
public static final int STATE_SUCCESS = 4;
|
||||||
|
public static final int STATE_SYSTEM_ERROR = 3;
|
||||||
|
|
||||||
|
// 构造导出工具
|
||||||
|
public BackupUtils(Context context) {
|
||||||
|
mTextExport = new TextExport(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 外部存储是否可用
|
||||||
|
private static boolean externalStorageAvailable
|
||||||
|
// 外部存储是否可用
|
||||||
|
private static boolean externalStorageAvailable() {
|
||||||
|
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成文件存储在SD卡上
|
||||||
|
private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append(Environment.getExternalStorageDirectory()); // 获取SD卡路径
|
||||||
|
sb.append(context.getString(filePathResId)); // 获取文件路径
|
||||||
|
File filedir = new File(sb.toString()); // 创建文件路径对象
|
||||||
|
sb.append(context.getString(
|
||||||
|
fileNameFormatResId,
|
||||||
|
DateFormat.format(context.getString(R.string.format_date_ymd),
|
||||||
|
System.currentTimeMillis())));
|
||||||
|
File file = new File(sb.toString());
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!filedir.exists()) {
|
||||||
|
filedir.mkdir(); // 如果路径不存在,则创建路径
|
||||||
|
}
|
||||||
|
if (!file.exists()) {
|
||||||
|
file.createNewFile(); // 如果文件不存在,则创建文件
|
||||||
|
}
|
||||||
|
return file;
|
||||||
|
} catch (SecurityException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue