Merge branch 'master' of https://bdgit.educoder.net/pfqs8cjuk/gitProject
	
		
	
				
					
				
			
						commit
						d6d45592e6
					
				| @ -0,0 +1,232 @@ | |||||||
|  | package net.micode.notes.model; | ||||||
|  |   | ||||||
|  | import android.content.ContentProviderOperation;//批量的更新、插入、删除数据。
 | ||||||
|  | import android.content.ContentProviderResult;//操作的结果
 | ||||||
|  | import android.content.ContentUris;//用于添加和获取Uri后面的ID
 | ||||||
|  | import android.content.ContentValues;//一种用来存储基本数据类型数据的存储机制
 | ||||||
|  | import android.content.Context;//需要用该类来弄清楚调用者的实例
 | ||||||
|  | import android.content.OperationApplicationException;//操作应用程序容错
 | ||||||
|  | import android.net.Uri;//表示待操作的数据
 | ||||||
|  | import android.os.RemoteException;//远程容错
 | ||||||
|  | import android.util.Log;//输出日志,比如说出错、警告等
 | ||||||
|  |   | ||||||
|  | public class Note { | ||||||
|  |  //   private ContentValues mNoteDiffValues;
 | ||||||
|  |     ContentValues mNoteDiffValues;//
 | ||||||
|  |     private NoteData mNoteData; | ||||||
|  |     private static final String TAG = "Note"; | ||||||
|  |      | ||||||
|  |     /** | ||||||
|  |      * Create a new note id for adding a new note to databases | ||||||
|  |      */ | ||||||
|  |     public static synchronized long getNewNoteId(Context context, long folderId) { | ||||||
|  |         // Create a new note in the database
 | ||||||
|  |         ContentValues values = new ContentValues(); | ||||||
|  |         long createdTime = System.currentTimeMillis(); | ||||||
|  |         values.put(NoteColumns.CREATED_DATE, createdTime); | ||||||
|  |         values.put(NoteColumns.MODIFIED_DATE, createdTime); | ||||||
|  |         values.put(NoteColumns.TYPE, Notes.TYPE_NOTE); | ||||||
|  |         values.put(NoteColumns.LOCAL_MODIFIED, 1); | ||||||
|  |         values.put(NoteColumns.PARENT_ID, folderId);//将数据写入数据库表格
 | ||||||
|  |         Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values); | ||||||
|  |         //ContentResolver()主要是实现外部应用对ContentProvider中的数据
 | ||||||
|  |         //进行添加、删除、修改和查询操作
 | ||||||
|  |         long noteId = 0; | ||||||
|  |         try { | ||||||
|  |             noteId = Long.valueOf(uri.getPathSegments().get(1)); | ||||||
|  |         } catch (NumberFormatException e) { | ||||||
|  |             Log.e(TAG, "Get note id error :" + e.toString()); | ||||||
|  |             noteId = 0; | ||||||
|  |         }//try-catch异常处理
 | ||||||
|  |         if (noteId == -1) { | ||||||
|  |             throw new IllegalStateException("Wrong note id:" + noteId); | ||||||
|  |         } | ||||||
|  |         return noteId; | ||||||
|  |     } | ||||||
|  |   | ||||||
|  |     public Note() { | ||||||
|  |         mNoteDiffValues = new ContentValues(); | ||||||
|  |         mNoteData = new NoteData(); | ||||||
|  |     }//定义两个变量用来存储便签的数据,一个是存储便签属性、一个是存储便签内容
 | ||||||
|  |   | ||||||
|  |     public void setNoteValue(String key, String value) { | ||||||
|  |         mNoteDiffValues.put(key, value); | ||||||
|  |         mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); | ||||||
|  |         mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); | ||||||
|  |     }//设置数据库表格的标签属性数据
 | ||||||
|  |   | ||||||
|  |     public void setTextData(String key, String value) { | ||||||
|  |         mNoteData.setTextData(key, value); | ||||||
|  |     }//设置数据库表格的标签文本内容的数据
 | ||||||
|  |   | ||||||
|  |     public void setTextDataId(long id) { | ||||||
|  |         mNoteData.setTextDataId(id); | ||||||
|  |     }//设置文本数据的ID
 | ||||||
|  |   | ||||||
|  |     public long getTextDataId() { | ||||||
|  |         return mNoteData.mTextDataId; | ||||||
|  |     }//得到文本数据的ID
 | ||||||
|  |   | ||||||
|  |     public void setCallDataId(long id) { | ||||||
|  |         mNoteData.setCallDataId(id); | ||||||
|  |     }//设置电话号码数据的ID
 | ||||||
|  |   | ||||||
|  |     public void setCallData(String key, String value) { | ||||||
|  |         mNoteData.setCallData(key, value); | ||||||
|  |     }//得到电话号码数据的ID
 | ||||||
|  |   | ||||||
|  |     public boolean isLocalModified() { | ||||||
|  |         return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified(); | ||||||
|  |     }//判断是否是本地修改
 | ||||||
|  |   | ||||||
|  |     public boolean syncNote(Context context, long noteId) { | ||||||
|  |         if (noteId <= 0) { | ||||||
|  |             throw new IllegalArgumentException("Wrong note id:" + noteId); | ||||||
|  |         } | ||||||
|  |   | ||||||
|  |         if (!isLocalModified()) { | ||||||
|  |             return true; | ||||||
|  |         } | ||||||
|  |   | ||||||
|  |         /** | ||||||
|  |          * In theory, once data changed, the note should be updated on {@link NoteColumns#LOCAL_MODIFIED} and | ||||||
|  |          * {@link NoteColumns#MODIFIED_DATE}. For data safety, though update note fails, we also update the | ||||||
|  |          * note data info | ||||||
|  |          */ | ||||||
|  |         if (context.getContentResolver().update( | ||||||
|  |                 ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null, | ||||||
|  |                 null) == 0) { | ||||||
|  |             Log.e(TAG, "Update note error, should not happen"); | ||||||
|  |             // Do not return, fall through
 | ||||||
|  |         } | ||||||
|  |         mNoteDiffValues.clear(); | ||||||
|  |   | ||||||
|  |         if (mNoteData.isLocalModified() | ||||||
|  |                 && (mNoteData.pushIntoContentResolver(context, noteId) == null)) { | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |   | ||||||
|  |         return true; | ||||||
|  |     }//判断数据是否同步
 | ||||||
|  |   | ||||||
|  |     private class NoteData {//定义一个基本的便签内容的数据类,主要包含文本数据和电话号码数据
 | ||||||
|  |         private long mTextDataId; | ||||||
|  |   | ||||||
|  |         private ContentValues mTextDataValues;//文本数据
 | ||||||
|  |   | ||||||
|  |         private long mCallDataId; | ||||||
|  |   | ||||||
|  |         private ContentValues mCallDataValues;//电话号码数据
 | ||||||
|  |   | ||||||
|  |         private static final String TAG = "NoteData"; | ||||||
|  |   | ||||||
|  |         public NoteData() { | ||||||
|  |             mTextDataValues = new ContentValues(); | ||||||
|  |             mCallDataValues = new ContentValues(); | ||||||
|  |             mTextDataId = 0; | ||||||
|  |             mCallDataId = 0; | ||||||
|  |         } | ||||||
|  |         //下面是上述几个函数的具体实现
 | ||||||
|  |         boolean isLocalModified() { | ||||||
|  |             return mTextDataValues.size() > 0 || mCallDataValues.size() > 0; | ||||||
|  |         } | ||||||
|  |   | ||||||
|  |         void setTextDataId(long id) { | ||||||
|  |             if(id <= 0) { | ||||||
|  |                 throw new IllegalArgumentException("Text data id should larger than 0"); | ||||||
|  |             } | ||||||
|  |             mTextDataId = id; | ||||||
|  |         } | ||||||
|  |   | ||||||
|  |         void setCallDataId(long id) { | ||||||
|  |             if (id <= 0) { | ||||||
|  |                 throw new IllegalArgumentException("Call data id should larger than 0"); | ||||||
|  |             } | ||||||
|  |             mCallDataId = id; | ||||||
|  |         } | ||||||
|  |   | ||||||
|  |         void setCallData(String key, String value) { | ||||||
|  |             mCallDataValues.put(key, value); | ||||||
|  |             mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); | ||||||
|  |             mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); | ||||||
|  |         } | ||||||
|  |   | ||||||
|  |         void setTextData(String key, String value) { | ||||||
|  |             mTextDataValues.put(key, value); | ||||||
|  |             mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); | ||||||
|  |             mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); | ||||||
|  |         } | ||||||
|  |         //下面函数的作用是将新的数据通过Uri的操作存储到数据库
 | ||||||
|  |         Uri pushIntoContentResolver(Context context, long noteId) { | ||||||
|  |             /** | ||||||
|  |              * Check for safety | ||||||
|  |              */ | ||||||
|  |             if (noteId <= 0) { | ||||||
|  |                 throw new IllegalArgumentException("Wrong note id:" + noteId); | ||||||
|  |             }//判断数据是否合法
 | ||||||
|  |   | ||||||
|  |             ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); | ||||||
|  |             ContentProviderOperation.Builder builder = null;//数据库的操作列表
 | ||||||
|  |   | ||||||
|  |             if(mTextDataValues.size() > 0) { | ||||||
|  |                 mTextDataValues.put(DataColumns.NOTE_ID, noteId); | ||||||
|  |                 if (mTextDataId == 0) { | ||||||
|  |                     mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); | ||||||
|  |                     Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, | ||||||
|  |                             mTextDataValues); | ||||||
|  |                     try { | ||||||
|  |                         setTextDataId(Long.valueOf(uri.getPathSegments().get(1))); | ||||||
|  |                     } catch (NumberFormatException e) { | ||||||
|  |                         Log.e(TAG, "Insert new text data fail with noteId" + noteId); | ||||||
|  |                         mTextDataValues.clear(); | ||||||
|  |                         return null; | ||||||
|  |                     } | ||||||
|  |                 } else { | ||||||
|  |                     builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( | ||||||
|  |                             Notes.CONTENT_DATA_URI, mTextDataId)); | ||||||
|  |                     builder.withValues(mTextDataValues); | ||||||
|  |                     operationList.add(builder.build()); | ||||||
|  |                 } | ||||||
|  |                 mTextDataValues.clear(); | ||||||
|  |             }//把文本数据存入DataColumns
 | ||||||
|  |   | ||||||
|  |             if(mCallDataValues.size() > 0) { | ||||||
|  |                 mCallDataValues.put(DataColumns.NOTE_ID, noteId); | ||||||
|  |                 if (mCallDataId == 0) { | ||||||
|  |                     mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE); | ||||||
|  |                     Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, | ||||||
|  |                             mCallDataValues); | ||||||
|  |                     try { | ||||||
|  |                         setCallDataId(Long.valueOf(uri.getPathSegments().get(1))); | ||||||
|  |                     } catch (NumberFormatException e) { | ||||||
|  |                         Log.e(TAG, "Insert new call data fail with noteId" + noteId); | ||||||
|  |                         mCallDataValues.clear(); | ||||||
|  |                         return null; | ||||||
|  |                     } | ||||||
|  |                 } else { | ||||||
|  |                     builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( | ||||||
|  |                             Notes.CONTENT_DATA_URI, mCallDataId)); | ||||||
|  |                     builder.withValues(mCallDataValues); | ||||||
|  |                     operationList.add(builder.build()); | ||||||
|  |                 } | ||||||
|  |                 mCallDataValues.clear(); | ||||||
|  |             }//把电话号码数据存入DataColumns
 | ||||||
|  |   | ||||||
|  |             if (operationList.size() > 0) { | ||||||
|  |                 try { | ||||||
|  |                     ContentProviderResult[] results = context.getContentResolver().applyBatch( | ||||||
|  |                             Notes.AUTHORITY, operationList); | ||||||
|  |                     return (results == null || results.length == 0 || results[0] == null) ? null | ||||||
|  |                             : ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId); | ||||||
|  |                 } catch (RemoteException e) { | ||||||
|  |                     Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); | ||||||
|  |                     return null; | ||||||
|  |                 } catch (OperationApplicationException e) { | ||||||
|  |                     Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); | ||||||
|  |                     return null; | ||||||
|  |                 } | ||||||
|  |             }//存储过程中的异常处理
 | ||||||
|  |             return null; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,116 @@ | |||||||
|  | .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; | ||||||
|  |     public static final int COLUMN_BG_COLOR_ID  = 1; | ||||||
|  |     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) { | ||||||
|  |                 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()); | ||||||
|  | 
 | ||||||
|  |                 Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]); | ||||||
|  |                 if (c != null && c.moveToFirst()) { | ||||||
|  |                     if (c.getCount() > 1) { | ||||||
|  |                         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(); | ||||||
|  | } | ||||||
					Loading…
					
					
				
		Reference in new issue