1.修改Notes.java中的常量接口为常量类,防止继承常量接口类的信息的流失,提高安全性,

2.给每个实用程序类添加一个构造函数
Signed-off-by: qianji <1535486312@qq.com>
a_branch
qianji 3 years ago
parent 5a63f3e5ab
commit 7d566fd63e

@ -18,6 +18,8 @@ package net.micode.notes.data;
import android.net.Uri;
public class Notes {
private static final String UTILITY_CLASS = "Utility class";
private static final String CONTENT = "content://";
public static final String AUTHORITY = "micode_notes";
public static final String TAG = "Notes";
public static final int TYPE_NOTE = 0;
@ -47,6 +49,10 @@ public class Notes {
public static final int TYPE_WIDGET_4X = 1;
public static class DataConstants {
private DataConstants()
{
throw new IllegalStateException(UTILITY_CLASS);
}
public static final String NOTE = TextNote.CONTENT_ITEM_TYPE;
public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE;
}
@ -54,14 +60,26 @@ public class Notes {
/**
* Uri to query all notes and folders
*/
public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note");
public static final Uri CONTENT_NOTE_URI = Uri.parse(CONTENT + AUTHORITY + "/note");
/**
* Uri to query data
*/
public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data");
public interface NoteColumns {
public static final Uri CONTENT_DATA_URI = Uri.parse(CONTENT + AUTHORITY + "/data");
//常量接口模式是对接口的一种不良使用。
//一个类在内部使用一些常量是一个实现细节。
/*
API
使使
*/
//解决方法:将常量接口改成常量类或者枚举类
public final class NoteColumns {
//实用程序类(实用程序类是定义一组执行通用功能的方法的类),不能用自带的构造函数,实用程序类一般都是包含的静态方法。 它通常是用来包装一些的“有用”的算法
private NoteColumns()
{
throw new IllegalStateException(UTILITY_CLASS);
}
/**
* The unique ID for a row
* <P> Type: INTEGER (long) </P>
@ -167,7 +185,11 @@ public class Notes {
public static final String VERSION = "version";
}
public interface DataColumns {
public final class DataColumns {
private DataColumns()
{
throw new IllegalStateException(UTILITY_CLASS);
}
/**
* The unique ID for a row
* <P> Type: INTEGER (long) </P>
@ -241,12 +263,16 @@ public class Notes {
public static final String DATA5 = "data5";
}
public static final class TextNote implements DataColumns {
public static final class TextNote {
private TextNote()
{
throw new IllegalStateException(UTILITY_CLASS);
}
/**
* Mode to indicate the text in check list mode or not
* <P> Type: Integer 1:check list mode 0: normal mode </P>
*/
public static final String MODE = DATA1;
public static final String MODE = DataColumns.DATA1;
public static final int MODE_CHECK_LIST = 1;
@ -254,26 +280,30 @@ public class Notes {
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/text_note";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note");
public static final Uri CONTENT_URI = Uri.parse(CONTENT + AUTHORITY + "/text_note");
}
public static final class CallNote implements DataColumns {
public static final class CallNote {
private CallNote()
{
throw new IllegalStateException(UTILITY_CLASS);
}
/**
* Call date for this record
* <P> Type: INTEGER (long) </P>
*/
public static final String CALL_DATE = DATA1;
public static final String CALL_DATE = DataColumns.DATA1;
/**
* Phone number for this record
* <P> Type: TEXT </P>
*/
public static final String PHONE_NUMBER = DATA3;
public static final String PHONE_NUMBER = DataColumns.DATA3;
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/call_note";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/call_note";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note");
public static final Uri CONTENT_URI = Uri.parse(CONTENT + AUTHORITY + "/call_note");
}
}

@ -35,6 +35,8 @@ import java.util.ArrayList;
public class Note {
//重复字符串定义为常量
private static final String WRONG_NOTE_ID = "Wrong note id:";
private ContentValues mNoteDiffValues;
private NoteData mNoteData;
private static final String TAG = "Note";
@ -60,7 +62,7 @@ public class Note {
noteId = 0;
}
if (noteId == -1) {
throw new IllegalStateException("Wrong note id:" + noteId);
throw new IllegalStateException(WRONG_NOTE_ID + noteId);
}
return noteId;
}
@ -102,7 +104,7 @@ public class Note {
public boolean syncNote(Context context, long noteId) {
if (noteId <= 0) {
throw new IllegalArgumentException("Wrong note id:" + noteId);
throw new IllegalArgumentException(WRONG_NOTE_ID + noteId);
}
if (!isLocalModified()) {
@ -183,7 +185,7 @@ public class Note {
* Check for safety
*/
if (noteId <= 0) {
throw new IllegalArgumentException("Wrong note id:" + noteId);
throw new IllegalArgumentException(WRONG_NOTE_ID + noteId);
}
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

Loading…
Cancel
Save