ADD file via upload

main
mxvwfs5gq 2 months ago
parent 746043cf79
commit 9cefd21afc

@ -0,0 +1,431 @@
/*
* 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.tool;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.os.RemoteException;
import android.util.Log;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.CallNote;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
/**
* -
* /
*/
public class DataUtils {
private static final String TAG = "DataUtils";
/**
*
*
* @param resolver
* @param ids ID
* @return truefalse
*/
public static boolean batchDeleteNotes(ContentResolver resolver, Set<Long> ids) {
// 参数有效性检查
if (resolver == null) {
Log.w(TAG, "ContentResolver is null");
return false;
}
if (ids == null || ids.isEmpty()) {
Log.d(TAG, "No notes to delete");
return true;
}
ArrayList<ContentProviderOperation> operations = new ArrayList<>(ids.size());
for (long id : ids) {
// 防止删除系统根文件夹
if (id == Notes.ID_ROOT_FOLDER) {
Log.w(TAG, "Skipping system root folder deletion");
continue;
}
// 添加删除操作
operations.add(ContentProviderOperation
.newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id))
.build());
}
if (operations.isEmpty()) {
Log.d(TAG, "No valid operations to execute");
return true;
}
try {
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operations);
return results != null && results.length > 0;
} catch (RemoteException | OperationApplicationException e) {
Log.e(TAG, "Batch delete failed", e);
}
return false;
}
/**
*
*
* @param resolver
* @param id ID
* @param srcFolderId ID
* @param desFolderId ID
*/
public static void moveNoteToFolder(ContentResolver resolver, long id, long srcFolderId, long desFolderId) {
if (resolver == null) {
Log.w(TAG, "ContentResolver is null");
return;
}
ContentValues values = new ContentValues();
values.put(NoteColumns.PARENT_ID, desFolderId);
values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId);
values.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记为本地已修改
resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null);
}
/**
*
*
* @param resolver
* @param ids ID
* @param folderId ID
* @return truefalse
*/
public static boolean batchMoveToFolder(ContentResolver resolver, Set<Long> ids, long folderId) {
if (resolver == null) {
Log.w(TAG, "ContentResolver is null");
return false;
}
if (ids == null || ids.isEmpty()) {
Log.d(TAG, "No notes to move");
return true;
}
ArrayList<ContentProviderOperation> operations = new ArrayList<>(ids.size());
for (long id : ids) {
operations.add(ContentProviderOperation
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id))
.withValue(NoteColumns.PARENT_ID, folderId)
.withValue(NoteColumns.LOCAL_MODIFIED, 1)
.build());
}
try {
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operations);
return results != null && results.length > 0;
} catch (RemoteException | OperationApplicationException e) {
Log.e(TAG, "Batch move failed", e);
}
return false;
}
/**
*
*
* @param resolver
* @return
*/
public static int getUserFolderCount(ContentResolver resolver) {
if (resolver == null) {
Log.w(TAG, "ContentResolver is null");
return 0;
}
String[] projection = {NoteColumns.ID};
String selection = NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?";
String[] selectionArgs = {
String.valueOf(Notes.TYPE_FOLDER),
String.valueOf(Notes.ID_TRASH_FOLDER) // 修正拼写错误FOLER -> FOLDER
};
try (Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI,
projection, selection, selectionArgs, null)) {
return cursor != null ? cursor.getCount() : 0;
}
}
/**
*
*
* @param resolver
* @param noteId ID
* @param type
* @return true
*/
public static boolean isNoteVisible(ContentResolver resolver, long noteId, int type) {
if (resolver == null) return false;
String selection = NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?";
String[] selectionArgs = {
String.valueOf(type),
String.valueOf(Notes.ID_TRASH_FOLDER)
};
try (Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
null, selection, selectionArgs, null)) {
return cursor != null && cursor.getCount() > 0;
}
}
/**
*
*
* @param resolver
* @param noteId ID
* @return true
*/
public static boolean doesNoteExist(ContentResolver resolver, long noteId) {
if (resolver == null) return false;
try (Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
null, null, null, null)) {
return cursor != null && cursor.getCount() > 0;
}
}
/**
*
*
* @param resolver
* @param dataId ID
* @return true
*/
public static boolean doesDataExist(ContentResolver resolver, long dataId) {
if (resolver == null) return false;
try (Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId),
null, null, null, null)) {
return cursor != null && cursor.getCount() > 0;
}
}
/**
*
*
* @param resolver
* @param name
* @return true
*/
public static boolean isFolderNameExist(ContentResolver resolver, String name) {
if (resolver == null || TextUtils.isEmpty(name)) return false;
String selection = NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER +
" AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLDER +
" AND " + NoteColumns.SNIPPET + "=?";
String[] selectionArgs = { name };
try (Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI,
null, selection, selectionArgs, null)) {
return cursor != null && cursor.getCount() > 0;
}
}
/**
*
*
* @param resolver
* @param folderId ID
* @return
*/
public static Set<AppWidgetAttribute> getFolderWidgets(ContentResolver resolver, long folderId) {
Set<AppWidgetAttribute> widgets = new HashSet<>();
if (resolver == null) return widgets;
String[] projection = { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE };
String selection = NoteColumns.PARENT_ID + "=?";
String[] selectionArgs = { String.valueOf(folderId) };
try (Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI,
projection, selection, selectionArgs, null)) {
if (cursor != null && cursor.moveToFirst()) {
do {
try {
AppWidgetAttribute widget = new AppWidgetAttribute();
widget.widgetId = cursor.getInt(0);
widget.widgetType = cursor.getInt(1);
widgets.add(widget);
} catch (Exception e) {
Log.e(TAG, "Error reading widget attributes", e);
}
} while (cursor.moveToNext());
}
}
return widgets;
}
/**
* ID
*
* @param resolver
* @param noteId ID
* @return
*/
public static String getCallNumber(ContentResolver resolver, long noteId) {
if (resolver == null) return "";
String[] projection = { CallNote.PHONE_NUMBER };
String selection = CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?";
String[] selectionArgs = {
String.valueOf(noteId),
CallNote.CONTENT_ITEM_TYPE
};
try (Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,
projection, selection, selectionArgs, null)) {
if (cursor != null && cursor.moveToFirst()) {
return cursor.getString(0);
}
} catch (Exception e) {
Log.e(TAG, "Error getting call number", e);
}
return "";
}
/**
* ID
*
* @param resolver
* @param phoneNumber
* @param callDate
* @return ID0
*/
public static long findNoteByCallDetails(ContentResolver resolver, String phoneNumber, long callDate) {
if (resolver == null || TextUtils.isEmpty(phoneNumber)) return 0;
String[] projection = { CallNote.NOTE_ID };
String selection = CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND " +
"PHONE_NUMBERS_EQUAL(" + CallNote.PHONE_NUMBER + ", ?)";
String[] selectionArgs = {
String.valueOf(callDate),
CallNote.CONTENT_ITEM_TYPE,
phoneNumber
};
try (Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,
projection, selection, selectionArgs, null)) {
if (cursor != null && cursor.moveToFirst()) {
return cursor.getLong(0);
}
} catch (Exception e) {
Log.e(TAG, "Error finding note by call details", e);
}
return 0;
}
/**
*
*
* @param resolver
* @param noteId ID
* @return
* @throws IllegalArgumentException
*/
public static String getNoteSnippet(ContentResolver resolver, long noteId) {
if (resolver == null) throw new IllegalArgumentException("Resolver is null");
String[] projection = { NoteColumns.SNIPPET };
String selection = NoteColumns.ID + "=?";
String[] selectionArgs = { String.valueOf(noteId) };
try (Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI,
projection, selection, selectionArgs, null)) {
if (cursor != null && cursor.moveToFirst()) {
return cursor.getString(0);
}
throw new IllegalArgumentException("Note not found with id: " + noteId);
}
}
/**
*
*
* @param snippet
* @return
*/
public static String formatSnippet(String snippet) {
if (snippet == null) return null;
String formatted = snippet.trim();
int newlineIndex = formatted.indexOf('\n');
return (newlineIndex != -1) ? formatted.substring(0, newlineIndex) : formatted;
}
/* ----------------- 新增功能 ----------------- */
/**
*
*
* @param resolver
* @param noteId ID
* @return true
*/
public static boolean safeDeleteNote(ContentResolver resolver, long noteId) {
Set<Long> ids = new HashSet<>(1);
ids.add(noteId);
return batchDeleteNotes(resolver, ids);
}
/**
*
*
* @param resolver
* @param ids ID
* @param modified (1-, 0-)
* @return true
*/
public static boolean batchMarkModified(ContentResolver resolver, Set<Long> ids, int modified) {
if (resolver == null) return false;
ArrayList<ContentProviderOperation> operations = new ArrayList<>(ids.size());
for (long id : ids) {
operations.add(ContentProviderOperation
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id))
.withValue(NoteColumns.LOCAL_MODIFIED, modified)
.build());
}
try {
resolver.applyBatch(Notes.AUTHORITY, operations);
return true;
} catch (Exception e) {
Log.e(TAG, "Batch mark modified failed", e);
}
return false;
}
}
Loading…
Cancel
Save