From 1c48a67ea5e213fe018eef1ec14f344fb5f3e584 Mon Sep 17 00:00:00 2001 From: wpx <2143423633@qq.com> Date: Tue, 11 Apr 2023 20:32:14 +0800 Subject: [PATCH 1/7] 4.11 --- .../Contact.java | 73 ++ .../GTaskASyncTask.java | 123 +++ .../GTaskClient.java | 585 +++++++++++++ .../GTaskManager.java | 800 ++++++++++++++++++ .../GTaskSyncService.java | 128 +++ .../Notes.java | 279 ++++++ .../NotesDatabaseHelper.java | 363 ++++++++ .../NotesProvider.java | 305 +++++++ 8 files changed, 2656 insertions(+) create mode 100644 doc/王培鑫小米便签精读部分/Contact.java create mode 100644 doc/王培鑫小米便签精读部分/GTaskASyncTask.java create mode 100644 doc/王培鑫小米便签精读部分/GTaskClient.java create mode 100644 doc/王培鑫小米便签精读部分/GTaskManager.java create mode 100644 doc/王培鑫小米便签精读部分/GTaskSyncService.java create mode 100644 doc/王培鑫小米便签精读部分/Notes.java create mode 100644 doc/王培鑫小米便签精读部分/NotesDatabaseHelper.java create mode 100644 doc/王培鑫小米便签精读部分/NotesProvider.java diff --git a/doc/王培鑫小米便签精读部分/Contact.java b/doc/王培鑫小米便签精读部分/Contact.java new file mode 100644 index 0000000..a940a4a --- /dev/null +++ b/doc/王培鑫小米便签精读部分/Contact.java @@ -0,0 +1,73 @@ +/* + * 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;//调用android自带的类 +import android.database.Cursor;//从数据库中导入Cursor +import android.provider.ContactsContract.CommonDataKinds.Phone;//获取联系人电话信息 +import android.provider.ContactsContract.Data;//存储联系人信息 +import android.telephony.PhoneNumberUtils;//格式化用户输入的电话号码 +import android.util.Log;//日志接口 + +import java.util.HashMap;//存储联系人信息 + +public class Contact {//联系人类 + private static HashMap sContactCache;//作为缓存,存储联系人信息 + private static final String TAG = "Contact";//定义字符串常量 + + 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();//初始化联系人缓存表 + } + + if(sContactCache.containsKey(phoneNumber)) { + return sContactCache.get(phoneNumber);//在数据库中查找联系人信息 + } + + String selection = CALLER_ID_SELECTION.replace("+", + PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));//将CALLER_ID_SELECTION的最后的+好替代为号码的后MIN_MATCH位 + Cursor cursor = context.getContentResolver().query(//从数据库中读取数据 + Data.CONTENT_URI, + new String [] { Phone.DISPLAY_NAME },//返回联系人姓名 + selection, + new String[] { phoneNumber }, + null);//判断查询结果,move ToFirst返回第一条 + + 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; + } + } +} diff --git a/doc/王培鑫小米便签精读部分/GTaskASyncTask.java b/doc/王培鑫小米便签精读部分/GTaskASyncTask.java new file mode 100644 index 0000000..46b6704 --- /dev/null +++ b/doc/王培鑫小米便签精读部分/GTaskASyncTask.java @@ -0,0 +1,123 @@ + +/* + * 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.gtask.remote;//实现Gtask异步操作过程 + +import android.app.Notification;//引用的包名 +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.os.AsyncTask; + +import net.micode.notes.R;//引入android自动生成的R类 +import net.micode.notes.ui.NotesListActivity;//自动生成的该程序包名下的R.java,专门用于管理二进制资源 +import net.micode.notes.ui.NotesPreferenceActivity; + + +public class GTaskASyncTask extends AsyncTask {//异步任务类:任务同步和取消,显示同步任务的进程、通知和结果 + + private static int GTASK_SYNC_NOTIFICATION_ID = 5234235;//static int变量存储GTask同步通知的ID。 + + public interface OnCompleteListener {//初始化异步功能 + void onComplete(); + } + + private Context mContext;//文本内容 + + private NotificationManager mNotifiManager;//对象: 通知管理器类的实例化 + + private GTaskManager mTaskManager;//实例化任务管理器 + + private OnCompleteListener mOnCompleteListener;//实例化是否完成的监听器 + + public GTaskASyncTask(Context context, OnCompleteListener listener) {// GTaskASyncTask类的构造函数 + mContext = context; + mOnCompleteListener = listener; + mNotifiManager = (NotificationManager) mContext//getSystemService是Activity的一个方法,可根据传入的参数获得应服务的对象。这里以Context的NOTIFICATION_SERVICE为对象。 + .getSystemService(Context.NOTIFICATION_SERVICE);//getSystemService是一种可根据传入的参数获得应服务的对象的端口函数。 + mTaskManager = GTaskManager.getInstance();单例模式创建类的实例 + } + + public void cancelSync() {//取消同步 + mTaskManager.cancelSync();//取消同步 + } + + public void publishProgess(String message) {//显示信息 + publishProgress(new String[] { + message + }); + } + + private void showNotification(int tickerId, String content) {//显示通知 + Notification notification = new Notification(R.drawable.notification, mContext//新建通知 + .getString(tickerId), System.currentTimeMillis()); + notification.defaults = Notification.DEFAULT_LIGHTS; + notification.flags = Notification.FLAG_AUTO_CANCEL; + PendingIntent pendingIntent; + if (tickerId != R.string.ticker_success) {//如果同步失败,就从系统取得一个用于启动NotesPreferenceActivity的PendingIntent对象。 + pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, + NotesPreferenceActivity.class), 0);//如果同步不成功,会从系统获取一个来启动NotesPreferenceActivity的对象 + + } else {// 若同步成功,则从系统中获得一个对象来启动NotesListActivity + pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, + NotesListActivity.class), 0); + } + notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content,//设置最新事件信息 + pendingIntent); + mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);//通过NotificationManager对象的notify()方法来执行一个notification的消息 + } + + @Override + protected Integer doInBackground(Void... unused) {//执行后台操作 + publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity + .getSyncAccountName(mContext))); + return mTaskManager.sync(mContext, this);//进行后台同步具体操作 + } + + @Override + protected void onProgressUpdate(String... progress) {//显示进度的更新 + showNotification(R.string.ticker_syncing, progress[0]); + if (mContext instanceof GTaskSyncService) {//mContext是否在GTaskSyncService类中 + ((GTaskSyncService) mContext).sendBroadcast(progress[0]); + } + } + + @Override + protected void onPostExecute(Integer result) {//设置任务,比如在用户界面显示一个进度条 + if (result == GTaskManager.STATE_SUCCESS) {//同步成功,显示相应的东西。 + showNotification(R.string.ticker_success, mContext.getString(//若同步成功,则显示成功及展示出同步的账户 + R.string.success_sync_account, mTaskManager.getSyncAccount())); + NotesPreferenceActivity.setLastSyncTime(mContext, System.currentTimeMillis());//设置当前最新同步时间 + } else if (result == GTaskManager.STATE_NETWORK_ERROR) {//如果网络错误则显示网络错误的通知 + showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_network)); + } else if (result == GTaskManager.STATE_INTERNAL_ERROR) {//网络错误导致同步错误。 + showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_internal)); + } else if (result == GTaskManager.STATE_SYNC_CANCELLED) {//如果同步被取消则显示其通知 + showNotification(R.string.ticker_cancel, mContext + .getString(R.string.error_sync_cancelled)); + } + if (mOnCompleteListener != null) {//若监听器为空,则创建新进程 + new Thread(new Runnable() {//创建新进程 + + public void run() { + mOnCompleteListener.onComplete();//执行完后调用,返回主线程中 + } + }).start(); + } + } +} diff --git a/doc/王培鑫小米便签精读部分/GTaskClient.java b/doc/王培鑫小米便签精读部分/GTaskClient.java new file mode 100644 index 0000000..c67dfdf --- /dev/null +++ b/doc/王培鑫小米便签精读部分/GTaskClient.java @@ -0,0 +1,585 @@ +/* + * 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.gtask.remote; + +import android.accounts.Account; +import android.accounts.AccountManager; +import android.accounts.AccountManagerFuture; +import android.app.Activity; +import android.os.Bundle; +import android.text.TextUtils; +import android.util.Log; + +import net.micode.notes.gtask.data.Node; +import net.micode.notes.gtask.data.Task; +import net.micode.notes.gtask.data.TaskList; +import net.micode.notes.gtask.exception.ActionFailureException; +import net.micode.notes.gtask.exception.NetworkFailureException; +import net.micode.notes.tool.GTaskStringUtils; +import net.micode.notes.ui.NotesPreferenceActivity; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.cookie.Cookie; +import org.apache.http.impl.client.BasicCookieStore; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.params.BasicHttpParams; +import org.apache.http.params.HttpConnectionParams; +import org.apache.http.params.HttpParams; +import org.apache.http.params.HttpProtocolParams; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.LinkedList; +import java.util.List; +import java.util.zip.GZIPInputStream; +import java.util.zip.Inflater; +import java.util.zip.InflaterInputStream; + + +public class GTaskClient { + private static final String TAG = GTaskClient.class.getSimpleName(); + + private static final String GTASK_URL = "https://mail.google.com/tasks/"; + + private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig"; + + private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig"; + + private static GTaskClient mInstance = null; + + private DefaultHttpClient mHttpClient; + + private String mGetUrl; + + private String mPostUrl; + + private long mClientVersion; + + private boolean mLoggedin; + + private long mLastLoginTime; + + private int mActionId; + + private Account mAccount; + + private JSONArray mUpdateArray; + + private GTaskClient() { + mHttpClient = null; + mGetUrl = GTASK_GET_URL; + mPostUrl = GTASK_POST_URL; + mClientVersion = -1; + mLoggedin = false; + mLastLoginTime = 0; + mActionId = 1; + mAccount = null; + mUpdateArray = null; + } + + public static synchronized GTaskClient getInstance() { + if (mInstance == null) { + mInstance = new GTaskClient(); + } + return mInstance; + } + + public boolean login(Activity activity) { + // we suppose that the cookie would expire after 5 minutes + // then we need to re-login + final long interval = 1000 * 60 * 5; + if (mLastLoginTime + interval < System.currentTimeMillis()) { + mLoggedin = false; + } + + // need to re-login after account switch + if (mLoggedin + && !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity + .getSyncAccountName(activity))) { + mLoggedin = false; + } + + if (mLoggedin) { + Log.d(TAG, "already logged in"); + return true; + } + + mLastLoginTime = System.currentTimeMillis(); + String authToken = loginGoogleAccount(activity, false); + if (authToken == null) { + Log.e(TAG, "login google account failed"); + return false; + } + + // login with custom domain if necessary + if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase() + .endsWith("googlemail.com"))) { + StringBuilder url = new StringBuilder(GTASK_URL).append("a/"); + int index = mAccount.name.indexOf('@') + 1; + String suffix = mAccount.name.substring(index); + url.append(suffix + "/"); + mGetUrl = url.toString() + "ig"; + mPostUrl = url.toString() + "r/ig"; + + if (tryToLoginGtask(activity, authToken)) { + mLoggedin = true; + } + } + + // try to login with google official url + if (!mLoggedin) { + mGetUrl = GTASK_GET_URL; + mPostUrl = GTASK_POST_URL; + if (!tryToLoginGtask(activity, authToken)) { + return false; + } + } + + mLoggedin = true; + return true; + } + + private String loginGoogleAccount(Activity activity, boolean invalidateToken) { + String authToken; + AccountManager accountManager = AccountManager.get(activity); + Account[] accounts = accountManager.getAccountsByType("com.google"); + + if (accounts.length == 0) { + Log.e(TAG, "there is no available google account"); + return null; + } + + String accountName = NotesPreferenceActivity.getSyncAccountName(activity); + Account account = null; + for (Account a : accounts) { + if (a.name.equals(accountName)) { + account = a; + break; + } + } + if (account != null) { + mAccount = account; + } else { + Log.e(TAG, "unable to get an account with the same name in the settings"); + return null; + } + + // get the token now + AccountManagerFuture accountManagerFuture = accountManager.getAuthToken(account, + "goanna_mobile", null, activity, null, null); + try { + Bundle authTokenBundle = accountManagerFuture.getResult(); + authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN); + if (invalidateToken) { + accountManager.invalidateAuthToken("com.google", authToken); + loginGoogleAccount(activity, false); + } + } catch (Exception e) { + Log.e(TAG, "get auth token failed"); + authToken = null; + } + + return authToken; + } + + private boolean tryToLoginGtask(Activity activity, String authToken) { + if (!loginGtask(authToken)) { + // maybe the auth token is out of date, now let's invalidate the + // token and try again + authToken = loginGoogleAccount(activity, true); + if (authToken == null) { + Log.e(TAG, "login google account failed"); + return false; + } + + if (!loginGtask(authToken)) { + Log.e(TAG, "login gtask failed"); + return false; + } + } + return true; + } + + private boolean loginGtask(String authToken) { + int timeoutConnection = 10000; + int timeoutSocket = 15000; + HttpParams httpParameters = new BasicHttpParams(); + HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); + HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); + mHttpClient = new DefaultHttpClient(httpParameters); + BasicCookieStore localBasicCookieStore = new BasicCookieStore(); + mHttpClient.setCookieStore(localBasicCookieStore); + HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false); + + // login gtask + try { + String loginUrl = mGetUrl + "?auth=" + authToken; + HttpGet httpGet = new HttpGet(loginUrl); + HttpResponse response = null; + response = mHttpClient.execute(httpGet); + + // get the cookie now + List cookies = mHttpClient.getCookieStore().getCookies(); + boolean hasAuthCookie = false; + for (Cookie cookie : cookies) { + if (cookie.getName().contains("GTL")) { + hasAuthCookie = true; + } + } + if (!hasAuthCookie) { + Log.w(TAG, "it seems that there is no auth cookie"); + } + + // get the client version + String resString = getResponseContent(response.getEntity()); + String jsBegin = "_setup("; + String jsEnd = ")}"; + int begin = resString.indexOf(jsBegin); + int end = resString.lastIndexOf(jsEnd); + String jsString = null; + if (begin != -1 && end != -1 && begin < end) { + jsString = resString.substring(begin + jsBegin.length(), end); + } + JSONObject js = new JSONObject(jsString); + mClientVersion = js.getLong("v"); + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + return false; + } catch (Exception e) { + // simply catch all exceptions + Log.e(TAG, "httpget gtask_url failed"); + return false; + } + + return true; + } + + private int getActionId() { + return mActionId++; + } + + private HttpPost createHttpPost() { + HttpPost httpPost = new HttpPost(mPostUrl); + httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); + httpPost.setHeader("AT", "1"); + return httpPost; + } + + private String getResponseContent(HttpEntity entity) throws IOException { + String contentEncoding = null; + if (entity.getContentEncoding() != null) { + contentEncoding = entity.getContentEncoding().getValue(); + Log.d(TAG, "encoding: " + contentEncoding); + } + + InputStream input = entity.getContent(); + if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) { + input = new GZIPInputStream(entity.getContent()); + } else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) { + Inflater inflater = new Inflater(true); + input = new InflaterInputStream(entity.getContent(), inflater); + } + + try { + InputStreamReader isr = new InputStreamReader(input); + BufferedReader br = new BufferedReader(isr); + StringBuilder sb = new StringBuilder(); + + while (true) { + String buff = br.readLine(); + if (buff == null) { + return sb.toString(); + } + sb = sb.append(buff); + } + } finally { + input.close(); + } + } + + private JSONObject postRequest(JSONObject js) throws NetworkFailureException { + if (!mLoggedin) { + Log.e(TAG, "please login first"); + throw new ActionFailureException("not logged in"); + } + + HttpPost httpPost = createHttpPost(); + try { + LinkedList list = new LinkedList(); + list.add(new BasicNameValuePair("r", js.toString())); + UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8"); + httpPost.setEntity(entity); + + // execute the post + HttpResponse response = mHttpClient.execute(httpPost); + String jsString = getResponseContent(response.getEntity()); + return new JSONObject(jsString); + + } catch (ClientProtocolException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new NetworkFailureException("postRequest failed"); + } catch (IOException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new NetworkFailureException("postRequest failed"); + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("unable to convert response content to jsonobject"); + } catch (Exception e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("error occurs when posting request"); + } + } + + public void createTask(Task task) throws NetworkFailureException { + commitUpdate(); + try { + JSONObject jsPost = new JSONObject(); + JSONArray actionList = new JSONArray(); + + // action_list + actionList.put(task.getCreateAction(getActionId())); + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); + + // client_version + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + // post + JSONObject jsResponse = postRequest(jsPost); + JSONObject jsResult = (JSONObject) jsResponse.getJSONArray( + GTaskStringUtils.GTASK_JSON_RESULTS).get(0); + task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("create task: handing jsonobject failed"); + } + } + + public void createTaskList(TaskList tasklist) throws NetworkFailureException { + commitUpdate(); + try { + JSONObject jsPost = new JSONObject(); + JSONArray actionList = new JSONArray(); + + // action_list + actionList.put(tasklist.getCreateAction(getActionId())); + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); + + // client version + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + // post + JSONObject jsResponse = postRequest(jsPost); + JSONObject jsResult = (JSONObject) jsResponse.getJSONArray( + GTaskStringUtils.GTASK_JSON_RESULTS).get(0); + tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("create tasklist: handing jsonobject failed"); + } + } + + public void commitUpdate() throws NetworkFailureException { + if (mUpdateArray != null) { + try { + JSONObject jsPost = new JSONObject(); + + // action_list + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray); + + // client_version + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + postRequest(jsPost); + mUpdateArray = null; + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("commit update: handing jsonobject failed"); + } + } + } + + public void addUpdateNode(Node node) throws NetworkFailureException { + if (node != null) { + // too many update items may result in an error + // set max to 10 items + if (mUpdateArray != null && mUpdateArray.length() > 10) { + commitUpdate(); + } + + if (mUpdateArray == null) + mUpdateArray = new JSONArray(); + mUpdateArray.put(node.getUpdateAction(getActionId())); + } + } + + public void moveTask(Task task, TaskList preParent, TaskList curParent) + throws NetworkFailureException { + commitUpdate(); + try { + JSONObject jsPost = new JSONObject(); + JSONArray actionList = new JSONArray(); + JSONObject action = new JSONObject(); + + // action_list + action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE); + action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); + action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid()); + if (preParent == curParent && task.getPriorSibling() != null) { + // put prioring_sibing_id only if moving within the tasklist and + // it is not the first one + action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling()); + } + action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid()); + action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid()); + if (preParent != curParent) { + // put the dest_list only if moving between tasklists + action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid()); + } + actionList.put(action); + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); + + // client_version + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + postRequest(jsPost); + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("move task: handing jsonobject failed"); + } + } + + public void deleteNode(Node node) throws NetworkFailureException { + commitUpdate(); + try { + JSONObject jsPost = new JSONObject(); + JSONArray actionList = new JSONArray(); + + // action_list + node.setDeleted(true); + actionList.put(node.getUpdateAction(getActionId())); + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); + + // client_version + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + postRequest(jsPost); + mUpdateArray = null; + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("delete node: handing jsonobject failed"); + } + } + + public JSONArray getTaskLists() throws NetworkFailureException { + if (!mLoggedin) { + Log.e(TAG, "please login first"); + throw new ActionFailureException("not logged in"); + } + + try { + HttpGet httpGet = new HttpGet(mGetUrl); + HttpResponse response = null; + response = mHttpClient.execute(httpGet); + + // get the task list + String resString = getResponseContent(response.getEntity()); + String jsBegin = "_setup("; + String jsEnd = ")}"; + int begin = resString.indexOf(jsBegin); + int end = resString.lastIndexOf(jsEnd); + String jsString = null; + if (begin != -1 && end != -1 && begin < end) { + jsString = resString.substring(begin + jsBegin.length(), end); + } + JSONObject js = new JSONObject(jsString); + return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS); + } catch (ClientProtocolException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new NetworkFailureException("gettasklists: httpget failed"); + } catch (IOException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new NetworkFailureException("gettasklists: httpget failed"); + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("get task lists: handing jasonobject failed"); + } + } + + public JSONArray getTaskList(String listGid) throws NetworkFailureException { + commitUpdate(); + try { + JSONObject jsPost = new JSONObject(); + JSONArray actionList = new JSONArray(); + JSONObject action = new JSONObject(); + + // action_list + action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL); + action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); + action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid); + action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false); + actionList.put(action); + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); + + // client_version + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + JSONObject jsResponse = postRequest(jsPost); + return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS); + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("get task list: handing jsonobject failed"); + } + } + + public Account getSyncAccount() { + return mAccount; + } + + public void resetUpdateArray() { + mUpdateArray = null; + } +} diff --git a/doc/王培鑫小米便签精读部分/GTaskManager.java b/doc/王培鑫小米便签精读部分/GTaskManager.java new file mode 100644 index 0000000..c453b05 --- /dev/null +++ b/doc/王培鑫小米便签精读部分/GTaskManager.java @@ -0,0 +1,800 @@ +/* + * 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.gtask.remote;//实现同步功能的主函数 + +import android.app.Activity;//在包里引用各种类 +import android.content.ContentResolver;//引用各种类 +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.util.Log; + +import net.micode.notes.R; +import net.micode.notes.data.Notes; +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.gtask.data.MetaData; +import net.micode.notes.gtask.data.Node; +import net.micode.notes.gtask.data.SqlNote; +import net.micode.notes.gtask.data.Task; +import net.micode.notes.gtask.data.TaskList; +import net.micode.notes.gtask.exception.ActionFailureException; +import net.micode.notes.gtask.exception.NetworkFailureException; +import net.micode.notes.tool.DataUtils; +import net.micode.notes.tool.GTaskStringUtils; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; + + +public class GTaskManager {//声明一个类,类名称的命名规范:所有单词的首字母大写 + private static final String TAG = GTaskManager.class.getSimpleName();// 定义了一系列静态变量来显示GTask当前的状态 + + public static final int STATE_SUCCESS = 0; + + public static final int STATE_NETWORK_ERROR = 1; + + public static final int STATE_INTERNAL_ERROR = 2; + + public static final int STATE_SYNC_IN_PROGRESS = 3; + + public static final int STATE_SYNC_CANCELLED = 4;//声明了5个静态变量来分别表示5种状态 + + private static GTaskManager mInstance = null;//private 定义一系列不可被外部的类访问的量 + + private Activity mActivity; + + private Context mContext; + + private ContentResolver mContentResolver; + + private boolean mSyncing; + + private boolean mCancelled; + + private HashMap mGTaskListHashMap; + + private HashMap mGTaskHashMap; + + private HashMap mMetaHashMap; + + private TaskList mMetaList; + + private HashSet mLocalDeleteIdMap; + + private HashMap mGidToNid; + + private HashMap mNidToGid; + + private GTaskManager() {//方法:类的构造函数,对其内部变量进行初始化 + mSyncing = false;//正在同步标识,false代表未同步 + mCancelled = false; + mGTaskListHashMap = new HashMap();//Hashmap的映射实现字符串到对象。 + mGTaskHashMap = new HashMap();//创建一个节点表 + mMetaHashMap = new HashMap();//创建一个删除本地ID的map + mMetaList = null;//创建一个删除本地ID的map + mLocalDeleteIdMap = new HashSet(); + mGidToNid = new HashMap();//建立一个google id到节点id的映射 + mNidToGid = new HashMap(); + } + + public static synchronized GTaskManager getInstance() {//用static synchronized控制类的所有实例的访问 + if (mInstance == null) { + mInstance = new GTaskManager(); + } + return mInstance; + } + + public synchronized void setActivityContext(Activity activity) {//获取当前的操作并更新至GTask中 + // used for getting authtoken + mActivity = activity; + } + + public int sync(Context context, GTaskASyncTask asyncTask) { + if (mSyncing) { + Log.d(TAG, "Sync is in progress"); + return STATE_SYNC_IN_PROGRESS; + } + mContext = context; + mContentResolver = mContext.getContentResolver();//对GTaskManager的属性进行更新 + mSyncing = true; + mCancelled = false; + mGTaskListHashMap.clear();//对各种结构进行清空 + mGTaskHashMap.clear(); + mMetaHashMap.clear(); + mLocalDeleteIdMap.clear(); + mGidToNid.clear(); + mNidToGid.clear(); + + try {//异常处理程序 + GTaskClient client = GTaskClient.getInstance();//实例化一个GTask用户对象 + client.resetUpdateArray();//重置更新操作。 + + // login google task + if (!mCancelled) {//代码块:登陆google task,如果mActivity登录不上则抛出一个异常 + if (!client.login(mActivity)) { + throw new NetworkFailureException("login google task failed"); + } + } + + // get the task list from google + asyncTask.publishProgess(mContext.getString(R.string.sync_progress_init_list)); + initGTaskList();//调用下面自定义的方法,初始化GTaskList + + // do content sync work + asyncTask.publishProgess(mContext.getString(R.string.sync_progress_syncing)); + syncContent();//进行同步操作 + } catch (NetworkFailureException e) {//处理各种异常 + Log.e(TAG, e.toString()); + return STATE_NETWORK_ERROR; + } catch (ActionFailureException e) {//操作未完成异常 + Log.e(TAG, e.toString()); + return STATE_INTERNAL_ERROR; + } catch (Exception e) {//内部错误异常 + Log.e(TAG, e.toString()); + e.printStackTrace(); + return STATE_INTERNAL_ERROR; + } finally {//结束后清空环境。 + mGTaskListHashMap.clear();//同步结束后清空环境 + mGTaskHashMap.clear(); + mMetaHashMap.clear(); + mLocalDeleteIdMap.clear(); + mGidToNid.clear(); + mNidToGid.clear(); + mSyncing = false; + } + + return mCancelled ? STATE_SYNC_CANCELLED : STATE_SUCCESS;//若在同步时操作未取消,则返回同步成功,否则返回同步操作取消 + } + + private void initGTaskList() throws NetworkFailureException {//初始化任务列表,其实就是从Google获取任务列表。 + if (mCancelled)//初始化任务列表,其实就是从Google获取任务列表。 + return; + GTaskClient client = GTaskClient.getInstance();// 实例化一个GTask用户对象 + try {//获取任务列表 + JSONArray jsTaskLists = client.getTaskLists();//获取任务列表 + + // init meta list first + mMetaList = null; + for (int i = 0; i < jsTaskLists.length(); i++) {//对于获取的每一个任务串jsonarray + JSONObject object = jsTaskLists.getJSONObject(i);//获得任务串的每一个结点和id名字 + String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID);//获取它的id + String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME);//获取它的名字 + + if (name + .equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) {//如果 name 等于 字符串 "[MIUI_Notes]" + "METADATA",执行if语句块 + mMetaList = new TaskList();//新建数组,并为新建的数组设定内容 + mMetaList.setContentByRemoteJSON(object); + + // load meta data + JSONArray jsMetas = client.getTaskList(gid);//读取元数据 + for (int j = 0; j < jsMetas.length(); j++) {//把jsMetas里的每一个有识别码的metaData都放到哈希表中 + object = (JSONObject) jsMetas.getJSONObject(j); + MetaData metaData = new MetaData(); + metaData.setContentByRemoteJSON(object); + if (metaData.isWorthSaving()) {//判断前面获取的元数据有无价值加入列表中 + mMetaList.addChildTask(metaData); + if (metaData.getGid() != null) {//操作getGid:取得组识别码函数 + mMetaHashMap.put(metaData.getRelatedGid(), metaData); + } + } + } + } + } + + // create meta list if not existed + if (mMetaList == null) {//如果元数据列表不存在,则在客户端创建一个 + mMetaList = new TaskList(); + mMetaList.setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + + GTaskStringUtils.FOLDER_META); + GTaskClient.getInstance().createTaskList(mMetaList); + } + + // init task list + for (int i = 0; i < jsTaskLists.length(); i++) {//初始化任务列表 + JSONObject object = jsTaskLists.getJSONObject(i); + String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID); + String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME); + + if (name.startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX) + && !name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + + GTaskStringUtils.FOLDER_META)) { + TaskList tasklist = new TaskList();//创建一个新的任务列表 + tasklist.setContentByRemoteJSON(object);//对任务列表的内容进行设置 + mGTaskListHashMap.put(gid, tasklist);//对任务列表的内容进行设置 + mGTaskHashMap.put(gid, tasklist); + + // load tasks + JSONArray jsTasks = client.getTaskList(gid);//获取任务id号 + for (int j = 0; j < jsTasks.length(); j++) { + object = (JSONObject) jsTasks.getJSONObject(j); + gid = object.getString(GTaskStringUtils.GTASK_JSON_ID); + Task task = new Task(); + task.setContentByRemoteJSON(object);//设置任务内容 + if (task.isWorthSaving()) {//判断该任务有无价值保存 + task.setMetaInfo(mMetaHashMap.get(gid)); + tasklist.addChildTask(task); + mGTaskHashMap.put(gid, task); + } + } + } + } + } catch (JSONException e) {//初始化时捕捉异常 + Log.e(TAG, e.toString());//获取异常类型和异常详细消息 + e.printStackTrace(); + throw new ActionFailureException("initGTaskList: handing JSONObject failed");//抛出异常,提交 JSONObject 失败 + } + } + + private void syncContent() throws NetworkFailureException {//实现内容同步的操作 + int syncType; + Cursor c = null; + String gid; + Node node; + + mLocalDeleteIdMap.clear(); + + if (mCancelled) {//对于本地已删除的便签采取的动作 + return; + } + + // for local deleted note + try {//代码块:对本地要删除的节点进行操作 + c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, + "(type<>? AND parent_id=?)", new String[] {//定位要删除的节点位置 + String.valueOf(Notes.TYPE_SYSTEM), String.valueOf(Notes.ID_TRASH_FOLER) + }, null); + if (c != null) {//代码块:若获取到的待删除便签不为空,则进行同步操作 + while (c.moveToNext()) {// 通过while用指针遍历所有结点 + gid = c.getString(SqlNote.GTASK_ID_COLUMN); + node = mGTaskHashMap.get(gid); + if (node != null) {//节点非空则从哈希表里删除,并进行同步操作 + mGTaskHashMap.remove(gid); + doContentSync(Node.SYNC_ACTION_DEL_REMOTE, node, c); + } + + mLocalDeleteIdMap.add(c.getLong(SqlNote.ID_COLUMN));//在本地删除记录中添加这一项记录 + } + } else {//若c结点为空,即寻找错误的时候,报错 + Log.w(TAG, "failed to query trash folder"); + } + } finally {//代码块:最后把c关闭并重置 + if (c != null) { + c.close(); + c = null; + } + } + + // sync folder first + syncFolder(); + + // for note existing in database + try {//代码块:对已存在于数据库的便签进行同步 + c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, + "(type=? AND parent_id<>?)", new String[] {//c指针指向待操作的位置 + String.valueOf(Notes.TYPE_NOTE), String.valueOf(Notes.ID_TRASH_FOLER) + }, NoteColumns.TYPE + " DESC"); + if (c != null) {//query语句的用法 + while (c.moveToNext()) {//遍历节点 + gid = c.getString(SqlNote.GTASK_ID_COLUMN); + node = mGTaskHashMap.get(gid);//获取待操作的便签的节点 + if (node != null) {//节点不为空则删除gid,重新建立节点到gid的联系 + mGTaskHashMap.remove(gid); + mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN));//建立google id到节点id的映射 + mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid);//建立Nid到Gid之间的映射表 + syncType = node.getSyncAction(c); + } else {//若为空,先判断sqlnote的trim是否为0,是则确定同步方式为add_remote,否则为del_local + if (c.getString(SqlNote.GTASK_ID_COLUMN).trim().length() == 0) {//若本地增加了内容,则远程也要增加内容 + // local add + syncType = Node.SYNC_ACTION_ADD_REMOTE;// 远程增加内容 + } else {//若本地删除了内容,则远程也要删除内容应的gid + // remote delete + syncType = Node.SYNC_ACTION_DEL_LOCAL;//本地删除 + } + } + doContentSync(syncType, node, c); + } + } else { + Log.w(TAG, "failed to query existing note in database");//查询失败时在日志中写回错误信息 + } + + } finally {//在最后关闭c并重置 + if (c != null) { + c.close(); + c = null; + } + } + + // go through remaining items + Iterator> iter = mGTaskHashMap.entrySet().iterator();//扫描剩下的项目,逐个进行同步 + while (iter.hasNext()) {//迭代 + Map.Entry entry = iter.next(); + node = entry.getValue(); + doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null); + } + + // mCancelled can be set by another thread, so we neet to check one by + // one + // clear local delete table + if (!mCancelled) {//终止标识有可能被其他进程改变,因此需要一个个进行检查 + if (!DataUtils.batchDeleteNotes(mContentResolver, mLocalDeleteIdMap)) { + throw new ActionFailureException("failed to batch-delete local deleted notes");//终止标识有可能被其他线程改变 + } + } + + // refresh local sync id + if (!mCancelled) {//更新同步表 + GTaskClient.getInstance().commitUpdate(); + refreshLocalSyncId(); + } + + } + + private void syncFolder() throws NetworkFailureException {//同步文件夹 + Cursor c = null; + String gid; + Node node; + int syncType; + + if (mCancelled) {//判断是否取消该操作 + return; + } + + // for root folder + try {//对于根文件夹 + c = mContentResolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, + Notes.ID_ROOT_FOLDER), SqlNote.PROJECTION_NOTE, null, null, null); + if (c != null) { + c.moveToNext(); + gid = c.getString(SqlNote.GTASK_ID_COLUMN);//获取指针指向内容对应的gid + node = mGTaskHashMap.get(gid); + if (node != null) {// 获取gid所代表的节点 + mGTaskHashMap.remove(gid); + mGidToNid.put(gid, (long) Notes.ID_ROOT_FOLDER);//将该节点的gid到nid的映射加入映射表 + mNidToGid.put((long) Notes.ID_ROOT_FOLDER, gid); + // for system folder, only update remote name if necessary + if (!node.getName().equals(//若当前访问的文件夹是系统文件夹则只需要更新 + GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT)) + doContentSync(Node.SYNC_ACTION_UPDATE_REMOTE, node, c); + } else { + doContentSync(Node.SYNC_ACTION_ADD_REMOTE, node, c);//若非系统文件夹则在远程进行增加节点操作 + } + } else { + Log.w(TAG, "failed to query root folder");//警告,询问根目录文件夹失败 + } + } finally {//结束操作之后,最后将指针关闭,并将指针置空。方法与之前相同 + if (c != null) { + c.close(); + c = null; + } + } + + // for call-note folder + try { + c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(_id=?)",//使指针指向文件夹的位置 + new String[] { + String.valueOf(Notes.ID_CALL_RECORD_FOLDER)//添加MIUI文件前缀 + }, null); + if (c != null) { + if (c.moveToNext()) { + gid = c.getString(SqlNote.GTASK_ID_COLUMN);//获取指针指向内容对应的gid + node = mGTaskHashMap.get(gid); + if (node != null) { + mGTaskHashMap.remove(gid); + mGidToNid.put(gid, (long) Notes.ID_CALL_RECORD_FOLDER);//将该节点的gid到nid的映射加入映射表 + mNidToGid.put((long) Notes.ID_CALL_RECORD_FOLDER, gid); + // for system folder, only update remote name if + // necessary + if (!node.getName().equals(//若当前访问的文件夹是系统文件夹则只需要更新 + GTaskStringUtils.MIUI_FOLDER_PREFFIX + + GTaskStringUtils.FOLDER_CALL_NOTE)) + doContentSync(Node.SYNC_ACTION_UPDATE_REMOTE, node, c); + } else { + doContentSync(Node.SYNC_ACTION_ADD_REMOTE, node, c);//若非系统文件夹则在远程进行增加节点操作 + } + } + } else { + Log.w(TAG, "failed to query call note folder");//警告,询问通讯便签文件夹失败 + } + } finally { + if (c != null) {//代码块:结束操作之后,将指针指向内容关闭并将指针置空 + c.close(); + c = null; + } + } + + // for local existing folders + try {//对于本地已存在的文件的操作 + c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, + "(type=? AND parent_id<>?)", new String[] { + String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER) + }, NoteColumns.TYPE + " DESC"); + if (c != null) { + while (c.moveToNext()) {//使指针遍历所有的文件夹 + gid = c.getString(SqlNote.GTASK_ID_COLUMN); + node = mGTaskHashMap.get(gid); + if (node != null) { + mGTaskHashMap.remove(gid); + mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN)); + mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid); + syncType = node.getSyncAction(c); + } else {//更新同步类型 + if (c.getString(SqlNote.GTASK_ID_COLUMN).trim().length() == 0) { + // local add + syncType = Node.SYNC_ACTION_ADD_REMOTE; + } else {//若远程删除了内容,则本地也要删除内容 + // remote delete + syncType = Node.SYNC_ACTION_DEL_LOCAL; + } + } + doContentSync(syncType, node, c);//进行同步操作 + } + } else { + Log.w(TAG, "failed to query existing folder"); + } + } finally {//代码块:结束操作之后,将指针指向内容关闭并将指针置空 + if (c != null) { + c.close(); + c = null; + } + } + + // for remote add folders + Iterator> iter = mGTaskListHashMap.entrySet().iterator();//对于在远程增添的内容,将其在本地同步 + while (iter.hasNext()) {//使用迭代器对远程增添的内容进行遍历 + Map.Entry entry = iter.next(); + gid = entry.getKey(); + node = entry.getValue(); + if (mGTaskHashMap.containsKey(gid)) {//获取gid对应的节点 + mGTaskHashMap.remove(gid); + doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null); + } + } + + if (!mCancelled) + GTaskClient.getInstance().commitUpdate();//如果没有取消,在GTsk的客户端进行实例的提交更新 + } + + private void doContentSync(int syncType, Node node, Cursor c) throws NetworkFailureException {//内容同步 + if (mCancelled) {//如果已经取消同步,直接返回 + return; + } + + MetaData meta; + switch (syncType) {//根据同步类型选择操作 + case Node.SYNC_ACTION_ADD_LOCAL: + addLocalNode(node); + break; + case Node.SYNC_ACTION_ADD_REMOTE://远程添加 + addRemoteNode(node, c); + break; + case Node.SYNC_ACTION_DEL_LOCAL://远程删除 + meta = mMetaHashMap.get(c.getString(SqlNote.GTASK_ID_COLUMN)); + if (meta != null) { + GTaskClient.getInstance().deleteNode(meta); + } + mLocalDeleteIdMap.add(c.getLong(SqlNote.ID_COLUMN)); + break; + case Node.SYNC_ACTION_DEL_REMOTE: + meta = mMetaHashMap.get(node.getGid()); + if (meta != null) { + GTaskClient.getInstance().deleteNode(meta); + } + GTaskClient.getInstance().deleteNode(node); + break; + case Node.SYNC_ACTION_UPDATE_LOCAL://更新远程数据 + updateLocalNode(node, c); + break; + case Node.SYNC_ACTION_UPDATE_REMOTE: + updateRemoteNode(node, c); + break; + case Node.SYNC_ACTION_UPDATE_CONFLICT://这个case是更新冲突 + // merging both modifications maybe a good idea + // right now just use local update simply + updateRemoteNode(node, c); + break; + case Node.SYNC_ACTION_NONE://空操作 + break; + case Node.SYNC_ACTION_ERROR://操作错误 + default: + throw new ActionFailureException("unkown sync action type");//抛出异常,未知的同步行为类型 + } + } + + private void addLocalNode(Node node) throws NetworkFailureException {//添加本地节点 + if (mCancelled) { + return; + } + + SqlNote sqlNote; + if (node instanceof TaskList) {//若待增添节点为任务列表中的节点,进一步操作 + if (node.getName().equals(//根目录 + GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT)) { + sqlNote = new SqlNote(mContext, Notes.ID_ROOT_FOLDER); + } else if (node.getName().equals(//判断是否在存电话的文件夹里 + GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_CALL_NOTE)) { + sqlNote = new SqlNote(mContext, Notes.ID_CALL_RECORD_FOLDER); + } else {//若没有存放的文件夹,则将其放在根文件夹中 + sqlNote = new SqlNote(mContext); + sqlNote.setContent(node.getLocalJSONFromContent()); + sqlNote.setParentId(Notes.ID_ROOT_FOLDER); + } + } else {//若待增添节点不是任务列表中的节点,进一步操作 + sqlNote = new SqlNote(mContext); + JSONObject js = node.getLocalJSONFromContent(); + try {//异常判断 + if (js.has(GTaskStringUtils.META_HEAD_NOTE)) { + JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);//获取对应便签的jsonobject对象 + if (note.has(NoteColumns.ID)) {//判断便签中是否有条目 + long id = note.getLong(NoteColumns.ID); + if (DataUtils.existInNoteDatabase(mContentResolver, id)) {//若便签条目的id已无效,在便签中将其删除 + // the id is not available, have to create a new one + note.remove(NoteColumns.ID); + } + } + } + + if (js.has(GTaskStringUtils.META_HEAD_DATA)) {//以下为判断便签中的数据条目 + JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA); + for (int i = 0; i < dataArray.length(); i++) {//依次删除存在的data的ID + JSONObject data = dataArray.getJSONObject(i); + if (data.has(DataColumns.ID)) { + long dataId = data.getLong(DataColumns.ID); + if (DataUtils.existInDataDatabase(mContentResolver, dataId)) { + // the data id is not available, have to create + // a new one + data.remove(DataColumns.ID); + } + } + } + + } + } catch (JSONException e) {//对于异常进行处理 + Log.w(TAG, e.toString());//获取异常类型和异常详细消息 + e.printStackTrace(); + } + sqlNote.setContent(js);//将之前的操作获取到的js更新至该节点中 + + Long parentId = mGidToNid.get(((Task) node).getParent().getGid());//找到父任务的ID号,并作为sqlNote的父任务的ID,没有父任务则报错 + if (parentId == null) {//当不能找到该任务上一级的id时报错 + Log.e(TAG, "cannot find task's parent id locally"); + throw new ActionFailureException("cannot add local node"); + } + sqlNote.setParentId(parentId.longValue()); + } + + // create the local node + sqlNote.setGtaskId(node.getGid());//用getGid函数获取node节点的Gid,用于设置本地Gtask的ID + sqlNote.commit(false);//更新本地便签 + + // update gid-nid mapping + mGidToNid.put(node.getGid(), sqlNote.getId());//更新gid与nid的映射表 + mNidToGid.put(sqlNote.getId(), node.getGid()); + + // update meta + updateRemoteMeta(node.getGid(), sqlNote);//更新远程的数据 + } + + private void updateLocalNode(Node node, Cursor c) throws NetworkFailureException {//更新远程的数据 + if (mCancelled) {//如果正在取消同步,直接返回 + return; + } + + SqlNote sqlNote; + // update the note locally + sqlNote = new SqlNote(mContext, c);//在指针指向处创建一个新的节点 + sqlNote.setContent(node.getLocalJSONFromContent());//利用待更新节点中的内容对数据库节点进行设置 + + Long parentId = (node instanceof Task) ? mGidToNid.get(((Task) node).getParent().getGid())//设置父任务的ID + : new Long(Notes.ID_ROOT_FOLDER); + if (parentId == null) {//当不能找到该任务上一级的id时报错 + Log.e(TAG, "cannot find task's parent id locally"); + throw new ActionFailureException("cannot update local node"); + } + sqlNote.setParentId(parentId.longValue());//设置该任务节点上一级的id + sqlNote.commit(true); + + // update meta info + updateRemoteMeta(node.getGid(), sqlNote);//更新远程的节点信息 + } + + private void addRemoteNode(Node node, Cursor c) throws NetworkFailureException {//添加远程节点 + if (mCancelled) {//如果正在取消同步,直接返回 + return; + } + + SqlNote sqlNote = new SqlNote(mContext, c);//新建一个sql节点并将内容存储进Node中 + Node n; + + // update remotely + if (sqlNote.isNoteType()) {//若待增添的节点为任务节点,进一步操作 + Task task = new Task(); + task.setContentByLocalJSON(sqlNote.getContent()); + + String parentGid = mNidToGid.get(sqlNote.getParentId());//当不能找到该任务上一级的id时报错 + if (parentGid == null) { + Log.e(TAG, "cannot find task's parent tasklist"); + throw new ActionFailureException("cannot add remote task"); + } + mGTaskListHashMap.get(parentGid).addChildTask(task); + + GTaskClient.getInstance().createTask(task); + n = (Node) task; + + // add meta + updateRemoteMeta(task.getGid(), sqlNote); + } else { + TaskList tasklist = null; + + // we need to skip folder if it has already existed + String folderName = GTaskStringUtils.MIUI_FOLDER_PREFFIX; + if (sqlNote.getId() == Notes.ID_ROOT_FOLDER)//按照文件夹的形式进行命名 + folderName += GTaskStringUtils.FOLDER_DEFAULT; + else if (sqlNote.getId() == Notes.ID_CALL_RECORD_FOLDER) + folderName += GTaskStringUtils.FOLDER_CALL_NOTE;//若为电话号码便签文件夹则按电话号码便签命名 + else + folderName += sqlNote.getSnippet(); + + Iterator> iter = mGTaskListHashMap.entrySet().iterator();//使用iterator作为map接口,对map进行遍历 + while (iter.hasNext()) {//iterator迭代器,通过统一的接口迭代所有的map元素 + Map.Entry entry = iter.next(); + String gid = entry.getKey(); + TaskList list = entry.getValue(); + + if (list.getName().equals(folderName)) { + tasklist = list; + if (mGTaskHashMap.containsKey(gid)) { + mGTaskHashMap.remove(gid); + } + break; + } + } + + // no match we can add now + if (tasklist == null) {//若找不到任务列表,则创建一个新的任务列表 + tasklist = new TaskList(); + tasklist.setContentByLocalJSON(sqlNote.getContent()); + GTaskClient.getInstance().createTaskList(tasklist); + mGTaskListHashMap.put(tasklist.getGid(), tasklist); + } + n = (Node) tasklist; + } + + // update local note + sqlNote.setGtaskId(n.getGid());//更新本地节点 + sqlNote.commit(false); + sqlNote.resetLocalModified(); + sqlNote.commit(true); + + // gid-id mapping + mGidToNid.put(n.getGid(), sqlNote.getId());//更新gid与nid的映射表 + mNidToGid.put(sqlNote.getId(), n.getGid()); + } + + private void updateRemoteNode(Node node, Cursor c) throws NetworkFailureException {//更新远程节点 + if (mCancelled) {//如果正在取消同步,直接返回 + return; + } + + SqlNote sqlNote = new SqlNote(mContext, c);//在指针指向处创建一个新的节点 + + // update remotely + node.setContentByLocalJSON(sqlNote.getContent());//远程更新 + GTaskClient.getInstance().addUpdateNode(node);//更新节点数据 + + // update meta + updateRemoteMeta(node.getGid(), sqlNote);//更新数据 + + // move task if necessary + if (sqlNote.isNoteType()) {//判断节点类型是否符合要求 + Task task = (Task) node; + TaskList preParentList = task.getParent(); + + String curParentGid = mNidToGid.get(sqlNote.getParentId()); + if (curParentGid == null) {//找不到当前任务的上一级任务列表,报错 + Log.e(TAG, "cannot find task's parent tasklist"); + throw new ActionFailureException("cannot update remote task"); + } + TaskList curParentList = mGTaskListHashMap.get(curParentGid); + + if (preParentList != curParentList) {//若两个上一级任务列表不一致,进行任务的移动,从之前的任务列表中移动到该列表中 + preParentList.removeChildTask(task); + curParentList.addChildTask(task); + GTaskClient.getInstance().moveTask(task, preParentList, curParentList); + } + } + + // clear local modified flag + sqlNote.resetLocalModified();//清除本地的modified flag + sqlNote.commit(true); + } + + private void updateRemoteMeta(String gid, SqlNote sqlNote) throws NetworkFailureException {//更新远程的元数据节点 + if (sqlNote != null && sqlNote.isNoteType()) {//判断节点类型是否符合,类型符合时才进行更新操作 + MetaData metaData = mMetaHashMap.get(gid); + if (metaData != null) {//若元数据组为空,则创建一个新的元数据组 + metaData.setMeta(gid, sqlNote.getContent()); + GTaskClient.getInstance().addUpdateNode(metaData); + } else {//若元数据组不为空,则进行更新 + metaData = new MetaData(); + metaData.setMeta(gid, sqlNote.getContent()); + mMetaList.addChildTask(metaData); + mMetaHashMap.put(gid, metaData); + GTaskClient.getInstance().createTask(metaData); + } + } + } + + private void refreshLocalSyncId() throws NetworkFailureException {//刷新本地便签id,从远程同步 + if (mCancelled) { + return; + } + + // get the latest gtask list + mGTaskHashMap.clear();//获取最新的任务列表 + mGTaskListHashMap.clear(); + mMetaHashMap.clear(); + initGTaskList(); + + Cursor c = null; + try { + c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, + "(type<>? AND parent_id<>?)", new String[] {//c作为光标定位,用query进行查询 + String.valueOf(Notes.TYPE_SYSTEM), String.valueOf(Notes.ID_TRASH_FOLER) + }, NoteColumns.TYPE + " DESC"); + if (c != null) { + while (c.moveToNext()) {//用迭代器不断从GTask的哈希表中删除node节点,设置新的content和新的resolcer并更新 + String gid = c.getString(SqlNote.GTASK_ID_COLUMN); + Node node = mGTaskHashMap.get(gid); + if (node != null) { + mGTaskHashMap.remove(gid); + ContentValues values = new ContentValues(); + values.put(NoteColumns.SYNC_ID, node.getLastModified());//在ContentValues中创建键值对。准备通过contentResolver写入数据 + mContentResolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, + c.getLong(SqlNote.ID_COLUMN)), values, null, null);//进行更新操作 + } else {//若发现节点为空则报错 + Log.e(TAG, "something is missed"); + throw new ActionFailureException( + "some local items don't have gid after sync"); + } + } + } else { + Log.w(TAG, "failed to query local note to refresh sync id"); + } + } finally {//结束操作之后,将指针指向内容关闭并将指针置空 + if (c != null) { + c.close(); + c = null; + } + } + } + + public String getSyncAccount() {//获取同步帐户 + return GTaskClient.getInstance().getSyncAccount().name; + } + + public void cancelSync() {//取消同步,置mCancelled为true + mCancelled = true; + } +} diff --git a/doc/王培鑫小米便签精读部分/GTaskSyncService.java b/doc/王培鑫小米便签精读部分/GTaskSyncService.java new file mode 100644 index 0000000..66f7780 --- /dev/null +++ b/doc/王培鑫小米便签精读部分/GTaskSyncService.java @@ -0,0 +1,128 @@ +/* + * 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.gtask.remote;//包名 + +import android.app.Activity;//以下几行都是引用基于数据库服务的类 +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.os.IBinder; + +public class GTaskSyncService extends Service {// 类:由Service组件扩展而来,作用是提供GTask的同步服务 + public final static String ACTION_STRING_NAME = "sync_action_type";//定义一系列静态变量 + + public final static int ACTION_START_SYNC = 0; + + public final static int ACTION_CANCEL_SYNC = 1; + + public final static int ACTION_INVALID = 2;//用数字0、1、2分别表示开始同步、取消同步、同步无效 + + public final static String GTASK_SERVICE_BROADCAST_NAME = "net.micode.notes.gtask.remote.gtask_sync_service";//广播名称 + + public final static String GTASK_SERVICE_BROADCAST_IS_SYNCING = "isSyncing";//表示广播正在同步 + + public final static String GTASK_SERVICE_BROADCAST_PROGRESS_MSG = "progressMsg";//进程消息 + + private static GTaskASyncTask mSyncTask = null; + + private static String mSyncProgress = ""; + + private void startSync() {//开始同步 + if (mSyncTask == null) { + mSyncTask = new GTaskASyncTask(this, new GTaskASyncTask.OnCompleteListener() { + public void onComplete() {//实现了在GTaskASyncTask类中定义的接口onComplete( ) + mSyncTask = null; + sendBroadcast(""); + stopSelf(); + } + }); + sendBroadcast(""); + mSyncTask.execute();//调用同步执行的函数 + } + } + + private void cancelSync() {//取消同步 + if (mSyncTask != null) { + mSyncTask.cancelSync(); + } + } + + @Override + public void onCreate() {//初始化一个service + mSyncTask = null; + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) {//告诉系统如何重启服务,如判断是否异常终止后重新启动,在何种情况下异常终止等。 + Bundle bundle = intent.getExtras(); + if (bundle != null && bundle.containsKey(ACTION_STRING_NAME)) {//判断当前的同步状态,根据开始或取消,执行对应操作 + switch (bundle.getInt(ACTION_STRING_NAME, ACTION_INVALID)) { + case ACTION_START_SYNC: + startSync(); + break; + case ACTION_CANCEL_SYNC: + cancelSync(); + break; + default: + break; + } + return START_STICKY; + } + return super.onStartCommand(intent, flags, startId);//调用父类的函数 + } + + @Override + public void onLowMemory() {//广播信息msg + if (mSyncTask != null) { + mSyncTask.cancelSync(); + } + } + + public IBinder onBind(Intent intent) {//service服务中的绑定操作 + return null; + } + + public void sendBroadcast(String msg) {//发送广播内容 + mSyncProgress = msg; + Intent intent = new Intent(GTASK_SERVICE_BROADCAST_NAME); + intent.putExtra(GTASK_SERVICE_BROADCAST_IS_SYNCING, mSyncTask != null); + intent.putExtra(GTASK_SERVICE_BROADCAST_PROGRESS_MSG, msg); + sendBroadcast(intent);//发送这个通知 + } + + public static void startSync(Activity activity) {//开始同步 + GTaskManager.getInstance().setActivityContext(activity); + Intent intent = new Intent(activity, GTaskSyncService.class); + intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_START_SYNC); + activity.startService(intent); + } + + public static void cancelSync(Context context) {//取消同步 + Intent intent = new Intent(context, GTaskSyncService.class); + intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_CANCEL_SYNC); + context.startService(intent); + } + + public static boolean isSyncing() {//判断当前是否处于同步状态 + return mSyncTask != null; + } + + public static String getProgressString() {//返回当前同步状态 + return mSyncProgress; + } +} diff --git a/doc/王培鑫小米便签精读部分/Notes.java b/doc/王培鑫小米便签精读部分/Notes.java new file mode 100644 index 0000000..830b041 --- /dev/null +++ b/doc/王培鑫小米便签精读部分/Notes.java @@ -0,0 +1,279 @@ +/* + * 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.net.Uri;//引用url +public class Notes {//Notes类,在类中定义了了许多常量 + public static final String AUTHORITY = "micode_notes"; + public static final String TAG = "Notes";//APP名称 + public static final int TYPE_NOTE = 0; + public static final int TYPE_FOLDER = 1; + public static final int TYPE_SYSTEM = 2; + + /** + * Following IDs are system folders' identifiers//下列标识为默认文件夹、临时文件夹、通话记录等 + * {@link Notes#ID_ROOT_FOLDER } is default folder + * {@link Notes#ID_TEMPARAY_FOLDER } is for notes belonging no folder + * {@link Notes#ID_CALL_RECORD_FOLDER} is to store call records + */ + public static final int ID_ROOT_FOLDER = 0;// 根目录 + public static final int ID_TEMPARAY_FOLDER = -1;//临时文件夹 + public static final int ID_CALL_RECORD_FOLDER = -2;//背景 + public static final int ID_TRASH_FOLER = -3;//回收站文件夹 + + public static final String INTENT_EXTRA_ALERT_DATE = "net.micode.notes.alert_date";//文件夹编号 + public static final String INTENT_EXTRA_BACKGROUND_ID = "net.micode.notes.background_color_id";//背景颜色 + public static final String INTENT_EXTRA_WIDGET_ID = "net.micode.notes.widget_id";//桌面插件 + public static final String INTENT_EXTRA_WIDGET_TYPE = "net.micode.notes.widget_type";// 大号桌面插件 + public static final String INTENT_EXTRA_FOLDER_ID = "net.micode.notes.folder_id";//创建text note的存放地址 + public static final String INTENT_EXTRA_CALL_DATE = "net.micode.notes.call_date";//定义call_data的ID + + public static final int TYPE_WIDGET_INVALIDE = -1;//定义查询便签和文件夹的指针 + public static final int TYPE_WIDGET_2X = 0;//定义查找数据的指针 + public static final int TYPE_WIDGET_4X = 1;// 定义桌面挂件的属性 + + public static class DataConstants {//DataContants类:存放textnotes和callnotes地址 + public static final String NOTE = TextNote.CONTENT_ITEM_TYPE; + public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE; + } + + /** + * Uri to query all notes and folders + */ + public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note");// URI常量,方便进行系统查询 + + /** + * Uri to query data + */ + public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data");//通过uri查询数据 + + public interface NoteColumns {//定义NoteColumns的常量,用于后面创建数据库的表头 + /** + * The unique ID for a row//主键 + *

Type: INTEGER (long)

+ */ + public static final String ID = "_id";//每一行的ID + + /** + * The parent's id for note or folder + *

Type: INTEGER (long)

+ */ + public static final String PARENT_ID = "parent_id"; + + /** + * Created data for note or folder//创建一个属于note或者folder的数据 + *

Type: INTEGER (long)

+ */ + public static final String CREATED_DATE = "created_date";//创建日期 + + /** + * Latest modified date + *

Type: INTEGER (long)

+ */ + public static final String MODIFIED_DATE = "modified_date";// 更改时间 + + + /** + * Alert date + *

Type: INTEGER (long)

+ */ + public static final String ALERTED_DATE = "alert_date"; + + /** + * Folder's name or text content of note//文件夹名字或者文件内容 + *

Type: TEXT

+ */ + public static final String SNIPPET = "snippet";//文件夹的名字或者便签内容 + + /** + * Note's widget id// note小物件id + *

Type: INTEGER (long)

+ */ + public static final String WIDGET_ID = "widget_id";//小部件ID + + /** + * Note's widget type//便签页数 + *

Type: INTEGER (long)

//note小物件类型 + */ + public static final String WIDGET_TYPE = "widget_type";//小部件类型 + + /** + * Note's background color's id//便签背景颜色 + *

Type: INTEGER (long)

//note背景颜色id + */ + public static final String BG_COLOR_ID = "bg_color_id";//背景颜色ID + + /** + * For text note, it doesn't has attachment, for multi-media + * note, it has at least one attachment + *

Type: INTEGER

//对于文字note,它没有附件,对于多个Note,它至少有一个附件 + */ + public static final String HAS_ATTACHMENT = "has_attachment";//是否有附件 + + /** + * Folder's count of notes + *

Type: INTEGER (long)

//文件夹内有多少个note + */ + public static final String NOTES_COUNT = "notes_count";//文件夹中的便签数量 + + /** + * The file type: folder or note//文件类型:folder 或者 note + *

Type: INTEGER

+ */ + public static final String TYPE = "type";//是文件夹还是便签 + + /** + * The last sync id + *

Type: INTEGER (long)

+ */ + public static final String SYNC_ID = "sync_id"; + + /** + * Sign to indicate local modified or not + *

Type: INTEGER

+ */ + public static final String LOCAL_MODIFIED = "local_modified";//标识是否本地修改 + + /** + * Original parent id before moving into temporary folder + *

Type : INTEGER

+ */ + public static final String ORIGIN_PARENT_ID = "origin_parent_id";//移动到临时文件夹之前的父文件夹 + + /** + * The gtask id + *

Type : TEXT

+ */ + public static final String GTASK_ID = "gtask_id";//后台任务ID + + /** + * The version code + *

Type : INTEGER (long)

+ */ + public static final String VERSION = "version";//版本 + } + + public interface DataColumns {// 定义DataColumns的常量,用于后面创建数据库的表头 + /** + * The unique ID for a row + *

Type: INTEGER (long)

+ */ + public static final String ID = "_id";//定义DataColumns的部分常量 + + /** + * The MIME type of the item represented by this row. + *

Type: Text

+ */ + public static final String MIME_TYPE = "mime_type";//定义文件类型 + + /** + * The reference id to note that this data belongs to + *

Type: INTEGER (long)

+ */ + public static final String NOTE_ID = "note_id";//便签ID + + /** + * Created data for note or folder + *

Type: INTEGER (long)

+ */ + public static final String CREATED_DATE = "created_date";//创建时间 + + /** + * Latest modified date//调整时间 + *

Type: INTEGER (long)

+ */ + public static final String MODIFIED_DATE = "modified_date";//最新的修改时间 + + /** + * Data's content + *

Type: TEXT

+ */ + public static final String CONTENT = "content";//定义数据包含的内容 + + + /** + * Generic data column, the meaning is {@link #MIMETYPE} specific, used for//根据MIME类型的不同,对应下面5个不同属性 + * integer data type + *

Type: INTEGER

+ */ + public static final String DATA1 = "data1";//文本内容的数据结构 + + /** + * Generic data column, the meaning is {@link #MIMETYPE} specific, used for + * integer data type + *

Type: INTEGER

+ */ + public static final String DATA2 = "data2";//文本模式 + + /** + * Generic data column, the meaning is {@link #MIMETYPE} specific, used for + * TEXT data type + *

Type: TEXT

+ */ + public static final String DATA3 = "data3"; + + /** + * Generic data column, the meaning is {@link #MIMETYPE} specific, used for + * TEXT data type + *

Type: TEXT

+ */ + public static final String DATA4 = "data4"; + + /** + * Generic data column, the meaning is {@link #MIMETYPE} specific, used for + * TEXT data type + *

Type: TEXT

+ */ + public static final String DATA5 = "data5"; + } + + public static final class TextNote implements DataColumns {// 对DataColumns的实现,建立一个新的类,textNote用于保存相关的信息。 + /** + * Mode to indicate the text in check list mode or not// 清单模式的MODE=0,正常模式的MODE=1 + *

Type: Integer 1:check list mode 0: normal mode

+ */ + public static final String MODE = DATA1;//电话号码 + + public static final int MODE_CHECK_LIST = 1;//设置为检查列表模式 + + public static final String CONTENT_TYPE = "vnd.android.cursor.dir/text_note";//内容的类型 + + 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 class CallNote implements DataColumns {//记录通话数据的表头 + /** + * Call date for this record//通话记录 + *

Type: INTEGER (long)

+ */ + public static final String CALL_DATE = DATA1;//存放通话时间信息 + + /** + * Phone number for this record + *

Type: TEXT

+ */ + public static final String PHONE_NUMBER = 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");//内容标识符 + } +} diff --git a/doc/王培鑫小米便签精读部分/NotesDatabaseHelper.java b/doc/王培鑫小米便签精读部分/NotesDatabaseHelper.java new file mode 100644 index 0000000..30577bf --- /dev/null +++ b/doc/王培鑫小米便签精读部分/NotesDatabaseHelper.java @@ -0,0 +1,363 @@ +/* + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)//该模块主要是用于存储Notes的数据,以及根据数据更改Notes结构体的变量 + * + * 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.ContentValues;//数据库操作中保存数据信息 +import android.content.Context;//访问资源和加载 +import android.database.sqlite.SQLiteDatabase;//提供了对应于添加、删除、更新、查询的操作 +import android.database.sqlite.SQLiteOpenHelper;//数据库的更新和创建 +import android.util.Log;//安卓日志接口 + +import net.micode.notes.data.Notes.DataColumns;//创建根目录文件夹、移动便签及清空垃圾文件夹 +import net.micode.notes.data.Notes.DataConstants;//移动便签 +import net.micode.notes.data.Notes.NoteColumns; + + +public class NotesDatabaseHelper extends SQLiteOpenHelper {// NotesDatabaseHelper类,继承于SQLiteOpenHelper类的子类。 + private static final String DB_NAME = "note.db";//定义数据库的名称为“note.db” + + private static final int DB_VERSION = 4;//数据库版本号 + + public interface TABLE {//将接口分成note和data + public static final String NOTE = "note";// 创建note表信息 + + public static final String DATA = "data";//创建data表信息 + } + + private static final String TAG = "NotesDatabaseHelper";//创建一个表格用来储存标签编号 + + private static NotesDatabaseHelper mInstance;//创建NotesDatabaseHelper的一个对象 + + private static final String CREATE_NOTE_TABLE_SQL =//增加文件夹 + "CREATE TABLE " + TABLE.NOTE + "(" +//定义.减少文件夹 + NoteColumns.ID + " INTEGER PRIMARY KEY," +//文件夹插入Note后需要更改的数据的表格 + NoteColumns.PARENT_ID + " INTEGER NOT NULL DEFAULT 0," +//id自增 + NoteColumns.ALERTED_DATE + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.BG_COLOR_ID + " INTEGER NOT NULL DEFAULT 0," +//应用界面颜色选择 + NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," +//创建时间数据 + NoteColumns.HAS_ATTACHMENT + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + + NoteColumns.NOTES_COUNT + " INTEGER NOT NULL DEFAULT 0," +//便签数量初始化为零 + NoteColumns.SNIPPET + " TEXT NOT NULL DEFAULT ''," +//Note中数据删除更改的数据表格 + NoteColumns.TYPE + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.WIDGET_ID + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.WIDGET_TYPE + " INTEGER NOT NULL DEFAULT -1," + + NoteColumns.SYNC_ID + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.LOCAL_MODIFIED + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.ORIGIN_PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''," + + NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" +// 数据库中需要存储的项目的名称,就相当于创建一个表格的表头的内容 + ")"; + + private static final String CREATE_DATA_TABLE_SQL =//这是用于创建note中数据的表项的SQL语句 + "CREATE TABLE " + TABLE.DATA + "(" +//note删除数据触发 + DataColumns.ID + " INTEGER PRIMARY KEY," + + DataColumns.MIME_TYPE + " TEXT NOT NULL," + + DataColumns.NOTE_ID + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + + NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + + DataColumns.CONTENT + " TEXT NOT NULL DEFAULT ''," + + DataColumns.DATA1 + " INTEGER," + + DataColumns.DATA2 + " INTEGER," + + DataColumns.DATA3 + " TEXT NOT NULL DEFAULT ''," + + DataColumns.DATA4 + " TEXT NOT NULL DEFAULT ''," + + DataColumns.DATA5 + " TEXT NOT NULL DEFAULT ''" + + ")";//文件夹删除note触发 + + private static final String CREATE_DATA_NOTE_ID_INDEX_SQL =//创建数据的ID的索引 + "CREATE INDEX IF NOT EXISTS note_id_index ON " + + TABLE.DATA + "(" + DataColumns.NOTE_ID + ");";//存储便签编号的一个数据表格 + + /** + * Increase folder's note count when move note to the folder//Increase增加文件 +Decrease减少文件 + */ + private static final String NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER =//当note移动到文件夹中时,文件夹的数量增加的功能 + "CREATE TRIGGER increase_folder_count_on_update "+ + " AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE + + " BEGIN " + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" + + " WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" + + " END";//在文件夹中移入一个Note之后需要更改的数据的表格 + + /** + * Decrease folder's note count when move note from folder + */ + private static final String NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER =//从文件夹中移出一个note时,更新数量 + "CREATE TRIGGER decrease_folder_count_on_update " + + " AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE + + " BEGIN " + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" + + " WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID + + " AND " + NoteColumns.NOTES_COUNT + ">0" + ";" + + " END";//在文件夹中插入一个Note之后需要更改的数据的表格 + + /** + * Increase folder's note count when insert new note to the folder//在文件夹中新增文件 + */ + private static final String NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER =//在文件夹中新建note时,更新数量 + "CREATE TRIGGER increase_folder_count_on_insert " + + " AFTER INSERT ON " + TABLE.NOTE + + " BEGIN " + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" + + " WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" + + " END";//在文件夹中插入一个Note之后需要更改的数据的表格 + + /** + * Decrease folder's note count when delete note from the folder//在文件夹中删除文件 + */ + private static final String NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER =//在文件夹中删除note时,更新文件夹的note数量 + "CREATE TRIGGER decrease_folder_count_on_delete " + + " AFTER DELETE ON " + TABLE.NOTE + + " BEGIN " + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" + + " WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID + + " AND " + NoteColumns.NOTES_COUNT + ">0;" + + " END";在文件夹中删除一个Note之后需要更改的数据的表格 + + /** + * Update note's content when insert data with type {@link DataConstants#NOTE}//在数据库中新增note或插入内容 + */ + private static final String DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER =//当note插入新的数据时,更新note的内容,并更改表格 + "CREATE TRIGGER update_note_content_on_insert " + + " AFTER INSERT ON " + TABLE.DATA + + " WHEN new." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" + + " BEGIN" + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT + + " WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" + + " END";// 在文件夹中对一个Note导入新的数据之后需要更改的数据的表格 + + /** + * Update note's content when data with {@link DataConstants#NOTE} type has changed//note发生变化时触发update + */ + private static final String DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER =//当note的数据发生变化时,更新表格 + "CREATE TRIGGER update_note_content_on_update " + + " AFTER UPDATE ON " + TABLE.DATA + + " WHEN old." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" + + " BEGIN" + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT + + " WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" + + " END";//Note数据被修改后需要更改的数据的表格 + + /** + * Update note's content when data with {@link DataConstants#NOTE} type has deleted//当类型被删除时,更新数据库 + */ + private static final String DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER =//当note的内容被删除时,更新表格的内容 + "CREATE TRIGGER update_note_content_on_delete " + + " AFTER delete ON " + TABLE.DATA + + " WHEN old." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" + + " BEGIN" +//当data删除之后对数据进行更新 + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.SNIPPET + "=''" + + " WHERE " + NoteColumns.ID + "=old." + DataColumns.NOTE_ID + ";" + + " END";// Note数据被删除后需要更改的数据的表格 + + /** + * Delete datas belong to note which has been deleted// 删除数据库中的数据,当这些数据在本地被删除 + */ + private static final String NOTE_DELETE_DATA_ON_DELETE_TRIGGER =//删除被删除的note的data + "CREATE TRIGGER delete_data_on_delete " + + " AFTER DELETE ON " + TABLE.NOTE + + " BEGIN" + + " DELETE FROM " + TABLE.DATA + + " WHERE " + DataColumns.NOTE_ID + "=old." + NoteColumns.ID + ";" + + " END"; + + /** + * Delete notes belong to folder which has been deleted + */ + private static final String FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER =//文件夹中删除文件,然后更改表格 + "CREATE TRIGGER folder_delete_notes_on_delete " + + " AFTER DELETE ON " + TABLE.NOTE + + " BEGIN" + + " DELETE FROM " + TABLE.NOTE + + " WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" + + " END";// 删除已删除的文件夹的便签后需要更改的数据的表格 + + /** + * Move notes belong to folder which has been moved to trash folder//移动笔记,当笔记从文件夹移动到垃圾文件夹时 + */ + private static final String FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER =//将垃圾文件夹里的note还原,并更新数据表格 + "CREATE TRIGGER folder_move_notes_on_trash " + + " AFTER UPDATE ON " + TABLE.NOTE + + " WHEN new." + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER + + " BEGIN" + + " UPDATE " + TABLE.NOTE + + " SET " + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER + + " WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" + + " END";// 还原垃圾桶中便签后需要更改的数据的表格 + + public NotesDatabaseHelper(Context context) {//传入数据库的名称和版本 + super(context, DB_NAME, null, DB_VERSION); + } + + public void createNoteTable(SQLiteDatabase db) {//创建数据库,储存属性 + db.execSQL(CREATE_NOTE_TABLE_SQL); + reCreateNoteTableTriggers(db); + createSystemFolder(db); + Log.d(TAG, "note table has been created");//调用系统输出调试信息,Log.d()是debug的意思。 + } + + private void reCreateNoteTableTriggers(SQLiteDatabase db) {//创建一个储存标签属性的表格 + db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_update");//创建增加文件夹的触发器 + db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_update");//创建减少文件夹的触发器 + db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_delete");//创建删除减少文件夹的触发器 + db.execSQL("DROP TRIGGER IF EXISTS delete_data_on_delete");//创建删除数据的触发器 + db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_insert");//创建插入文件夹的触发器 + db.execSQL("DROP TRIGGER IF EXISTS folder_delete_notes_on_delete");//创建删除note的触发器 + db.execSQL("DROP TRIGGER IF EXISTS folder_move_notes_on_trash");//创建移除note的触发器 + + db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER);//创建自己的数据库操作 + db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER);// 创建更新减少文件夹的触发器 + db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER);//创建删除减少文件夹的触发器 + db.execSQL(NOTE_DELETE_DATA_ON_DELETE_TRIGGER);//创建删除标记已删除数据的触发器 + db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER);// 创建插入文件夹的触发器 + db.execSQL(FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER);//创建删除notes的触发器 + db.execSQL(FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER);//创建将回收站中的notes还原的触发器 + } + + private void createSystemFolder(SQLiteDatabase db) {//创建系统文件夹 + ContentValues values = new ContentValues();//新建 ContantValues对象,储存基本数据类型 + + /** + * call record foler for call notes//创建callnote对应的文件夹 + */ + values.put(NoteColumns.ID, Notes.ID_CALL_RECORD_FOLDER);//将通话记录放入数据库 + values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); + db.insert(TABLE.NOTE, null, values); + + /** + * root folder which is default folder + */ + values.clear();//创建默认文件夹 + values.put(NoteColumns.ID, Notes.ID_ROOT_FOLDER);// 将数据库ID与通话记录文件夹ID形成映射 + values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);//将数据库中数据类型与通话记录文件夹类型形成映射 + db.insert(TABLE.NOTE, null, values);//插入ContantValues对象 + + /** + * temporary folder which is used for moving note//设置临时文件夹作为文件移动的有效目标 + */ + values.clear(); + values.put(NoteColumns.ID, Notes.ID_TEMPARAY_FOLDER); + values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); + db.insert(TABLE.NOTE, null, values); + + /** + * create trash folder + */ + values.clear(); + values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER); + values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); + db.insert(TABLE.NOTE, null, values); + } + + public void createDataTable(SQLiteDatabase db) {//创建数据表格,储存便签内容 + db.execSQL(CREATE_DATA_TABLE_SQL); + reCreateDataTableTriggers(db);//重新创建数据表的触发器 + db.execSQL(CREATE_DATA_NOTE_ID_INDEX_SQL); + Log.d(TAG, "data table has been created"); + } + + private void reCreateDataTableTriggers(SQLiteDatabase db) {//重新创建数据表格触发器 + db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_insert"); + db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_update"); + db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_delete"); + + db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER); + db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER); + db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER); + } + + static synchronized NotesDatabaseHelper getInstance(Context context) {//如果NotesDatabaseHelper创建失败,就再创建一个,采取static synchronized类型,防止多线程发生错误 + if (mInstance == null) { + mInstance = new NotesDatabaseHelper(context); + } + return mInstance; + } + + @Override + public void onCreate(SQLiteDatabase db) {//自动创建数据库 + createNoteTable(db); + createDataTable(db); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {//数据库版本的更新,包括数据库内容的更改 + boolean reCreateTriggers = false;//是否重建的信号 + boolean skipV2 = false;//是否从V2升级到V3 + + if (oldVersion == 1) {//判断当前版本是不是一代 + upgradeToV2(db); + skipV2 = true; // this upgrade including the upgrade from v2 to v3 + oldVersion++; + } + + if (oldVersion == 2 && !skipV2) {//判断旧版本是不是2号版本且没有跳过2号版本,就更新到3号版本 + upgradeToV3(db); + reCreateTriggers = true; + oldVersion++; + } + + if (oldVersion == 3) {//如果旧版本号为3号,就更新到4号版本 + upgradeToV4(db); + oldVersion++; + } + + if (reCreateTriggers) {//重新创建的同时,创建新的note table和datatable + reCreateNoteTableTriggers(db); + reCreateDataTableTriggers(db); + } + + if (oldVersion != newVersion) {//判断当前版本是不是最新的 + throw new IllegalStateException("Upgrade notes database to version " + newVersion + + "fails"); + } + } + + private void upgradeToV2(SQLiteDatabase db) {//将数据库的版本更新到V2 + db.execSQL("DROP TABLE IF EXISTS " + TABLE.NOTE); + db.execSQL("DROP TABLE IF EXISTS " + TABLE.DATA); + createNoteTable(db); + createDataTable(db); + } + + private void upgradeToV3(SQLiteDatabase db) {//数据库版本升级到V3 + // drop unused triggers + db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_insert"); + db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_delete"); + db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_update"); + // add a column for gtask id + db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.GTASK_ID + + " TEXT NOT NULL DEFAULT ''"); + // add a trash system folder + ContentValues values = new ContentValues();// 添加一个垃圾文件夹 + values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER);//为变量赋值 + values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); + db.insert(TABLE.NOTE, null, values); + } + + private void upgradeToV4(SQLiteDatabase db) {//数据库版本升级到V4 + db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.VERSION + + " INTEGER NOT NULL DEFAULT 0"); + } +} diff --git a/doc/王培鑫小米便签精读部分/NotesProvider.java b/doc/王培鑫小米便签精读部分/NotesProvider.java new file mode 100644 index 0000000..57e211a --- /dev/null +++ b/doc/王培鑫小米便签精读部分/NotesProvider.java @@ -0,0 +1,305 @@ +/* + * 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.app.SearchManager;//以下 引用android自带的类,包括数据库等 +import android.content.ContentProvider;//导入ContentProvider +import android.content.ContentUris;//导入ContentUris +import android.content.ContentValues;//导入contentvalues +import android.content.Intent;//导入intent +import android.content.UriMatcher;//导入UriMatcher +import android.database.Cursor;//查询数据库所需的游标 +import android.database.sqlite.SQLiteDatabase;//引入SQLite数据库 +import android.net.Uri; +import android.text.TextUtils;//文本识别处理工具 +import android.util.Log; + +import net.micode.notes.R;//访问、删除、插入、更新、扩展Notes的数据库 +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.data.NotesDatabaseHelper.TABLE; + + +public class NotesProvider extends ContentProvider {//为存储和获取数据提供接口。可以在不同的应用程序之间共享数据 + private static final UriMatcher mMatcher;//UriMatcher用于匹配Uri + + private NotesDatabaseHelper mHelper;//数据库辅助类的实例化 + + private static final String TAG = "NotesProvider";//该类的自定义标签 + + private static final int URI_NOTE = 1;//定义几种Uri的代号 + private static final int URI_NOTE_ITEM = 2;//注册Uri路径的过程 + private static final int URI_DATA = 3; + private static final int URI_DATA_ITEM = 4; + + private static final int URI_SEARCH = 5; + private static final int URI_SEARCH_SUGGEST = 6; + + static {//代码段:这是一个注册URI路径的过程 + mMatcher = new UriMatcher(UriMatcher.NO_MATCH); + mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE); + mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM); + mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA); + mMatcher.addURI(Notes.AUTHORITY, "data/#", URI_DATA_ITEM); + mMatcher.addURI(Notes.AUTHORITY, "search", URI_SEARCH); + mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, URI_SEARCH_SUGGEST);//SUGGEST_URI_PATH_QUERY 并不属于URI的一部分,而应是用于指向此路径的常量。 + mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", URI_SEARCH_SUGGEST); + }//注册完需要匹配的Uri后,就可以使用sMatcher.match(uri)方法对输入的Uri进行匹配 + + /** + * x'0A' represents the '\n' character in sqlite. For title and content in the search result,//trim()函数是去掉字符串头尾空格的函数 + * we will trim '\n' and white space in order to show more information. + */ + private static final String NOTES_SEARCH_PROJECTION = NoteColumns.ID + "," + + NoteColumns.ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA + ","// 设置表明额外数据、推荐显示的文本、图标、操作、数据 + + "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_1 + "," + + "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_2 + "," + + R.drawable.search_result + " AS " + SearchManager.SUGGEST_COLUMN_ICON_1 + "," + + "'" + Intent.ACTION_VIEW + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_ACTION + "," + + "'" + Notes.TextNote.CONTENT_TYPE + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA; + + private static String NOTES_SNIPPET_SEARCH_QUERY = "SELECT " + NOTES_SEARCH_PROJECTION// 便签的搜索、查询 + + " FROM " + TABLE.NOTE// 在特定表中某一字段检索特定子串 + + " WHERE " + NoteColumns.SNIPPET + " LIKE ?" + + " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + + " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE; + + @Override + public boolean onCreate() {//创建标签记录的表 + mHelper = NotesDatabaseHelper.getInstance(getContext()); + return true; + } + + @Override + public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,//查询uri在数据库中对应的位置 + String sortOrder) { + Cursor c = null;//创建空游 标 + SQLiteDatabase db = mHelper.getReadableDatabase();//获取可读数据库 + String id = null; + switch (mMatcher.match(uri)) {//匹配查找uri + case URI_NOTE: + c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null, + sortOrder); + break; + case URI_NOTE_ITEM://查询便签条目 + id = uri.getPathSegments().get(1); + c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id + + parseSelection(selection), selectionArgs, null, null, sortOrder); + break; + case URI_DATA://根据日期进行查询 + c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null, + sortOrder); + break; + case URI_DATA_ITEM: + id = uri.getPathSegments().get(1);//根据日期条目进行查询 + c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id + + parseSelection(selection), selectionArgs, null, null, sortOrder); + break; + case URI_SEARCH: + case URI_SEARCH_SUGGEST: + if (sortOrder != null || projection != null) {//不合法的参数 + throw new IllegalArgumentException( + "do not specify sortOrder, selection, selectionArgs, or projection" + "with this query"); + } + + String searchString = null; + if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) {//与搜索相关的ur + if (uri.getPathSegments().size() > 1) { + searchString = uri.getPathSegments().get(1); + } + } else { + searchString = uri.getQueryParameter("pattern");//截取Uri链接中的参数"pattern" + } + + if (TextUtils.isEmpty(searchString)) { + return null; + } + + try {//抛出异常 + searchString = String.format("%%%s%%", searchString); + c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY, + new String[] { searchString }); + } catch (IllegalStateException ex) { + Log.e(TAG, "got exception: " + ex.toString()); + } + break; + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + if (c != null) { + c.setNotificationUri(getContext().getContentResolver(), uri);//若getContentResolver发生变化,就接收通知 + } + return c; + } + + @Override + public Uri insert(Uri uri, ContentValues values) {//插入一个uri,uri是一个用于标识某一互联网资源名称的字符串 + SQLiteDatabase db = mHelper.getWritableDatabase();//获得可写的数据库 + long dataId = 0, noteId = 0, insertedId = 0; + switch (mMatcher.match(uri)) {//数据库的插入,用来存放数据 + case URI_NOTE: + insertedId = noteId = db.insert(TABLE.NOTE, null, values); + break; + case URI_DATA: + if (values.containsKey(DataColumns.NOTE_ID)) {//匹配note的ID + noteId = values.getAsLong(DataColumns.NOTE_ID); + } else { + Log.d(TAG, "Wrong data format without note id:" + values.toString()); + }//Log用于显示,格式错误 + insertedId = dataId = db.insert(TABLE.DATA, null, values);//插入便签数据 + break; + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + // Notify the note uri + if (noteId > 0) {//更新内容 + getContext().getContentResolver().notifyChange( + ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null); + } + + // Notify the data uri + if (dataId > 0) {//更新日期 + getContext().getContentResolver().notifyChange( + ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null); + } + + return ContentUris.withAppendedId(uri, insertedId);//最后返回插入uri的路径 + } + + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) {//删除一个uri + int count = 0; + String id = null; + SQLiteDatabase db = mHelper.getWritableDatabase();//获得可写的数据库 + boolean deleteData = false; + switch (mMatcher.match(uri)) {//查找匹配的uri + case URI_NOTE: + selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 "; + count = db.delete(TABLE.NOTE, selection, selectionArgs); + break; + case URI_NOTE_ITEM: + id = uri.getPathSegments().get(1); + /** + * ID that smaller than 0 is system folder which is not allowed to + * trash + */ + long noteId = Long.valueOf(id); + if (noteId <= 0) {//ID小于0表示是系统文件夹,不能删除 + break; + } + count = db.delete(TABLE.NOTE, + NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs); + break; + case URI_DATA: + count = db.delete(TABLE.DATA, selection, selectionArgs);//删除内容 + deleteData = true; + break; + case URI_DATA_ITEM: + id = uri.getPathSegments().get(1);//通过数据条目查找到 + count = db.delete(TABLE.DATA, + DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs); + deleteData = true; + break; + default: + throw new IllegalArgumentException("Unknown URI " + uri);//未知uri,则报错,显示Unknown URI + } + if (count > 0) {//对上述的操作进行判断,上述更改发生,count>0 + if (deleteData) {//如果真的删除了数据,则要求监听器对观察者发送通知 + getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);//所有修改要对观察者进行提醒 + } + getContext().getContentResolver().notifyChange(uri, null);//对所有修改进行通知 + } + return count; + } + + @Override + public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {//数据库更新uri + int count = 0; + String id = null; + SQLiteDatabase db = mHelper.getWritableDatabase();//获取可写数据库 + boolean updateData = false; + switch (mMatcher.match(uri)) {//根据不同的uri,更新数据库 + case URI_NOTE: + increaseNoteVersion(-1, selection, selectionArgs);//升级note版本 + count = db.update(TABLE.NOTE, values, selection, selectionArgs);//数据更新 + break; + case URI_NOTE_ITEM: + id = uri.getPathSegments().get(1); + increaseNoteVersion(Long.valueOf(id), selection, selectionArgs); + count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id + + parseSelection(selection), selectionArgs); + break; + case URI_DATA: + count = db.update(TABLE.DATA, values, selection, selectionArgs);//对该data的uri进行更新 + updateData = true; + break; + case URI_DATA_ITEM: + id = uri.getPathSegments().get(1);//获取该项目id并将其uri更新 + count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id + + parseSelection(selection), selectionArgs); + updateData = true; + break; + default: + throw new IllegalArgumentException("Unknown URI " + uri);// 抛出错误:未知uri + } + + if (count > 0) {//如果发生了更改,进行通知 + if (updateData) {//data更新 + getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null); + } + getContext().getContentResolver().notifyChange(uri, null); + } + return count; + } + + private String parseSelection(String selection) {//返回字符串规定格式 + return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); + } + + private void increaseNoteVersion(long id, String selection, String[] selectionArgs) {//升级note的版本 + StringBuilder sql = new StringBuilder(120);//创建stringbuilder对象 + sql.append("UPDATE "); + sql.append(TABLE.NOTE); + sql.append(" SET "); + sql.append(NoteColumns.VERSION); + sql.append("=" + NoteColumns.VERSION + "+1 "); + + if (id > 0 || !TextUtils.isEmpty(selection)) {// 如果id>0且selection不为空,添加“WHERE” + sql.append(" WHERE "); + } + if (id > 0) { + sql.append(NoteColumns.ID + "=" + String.valueOf(id)); + } + if (!TextUtils.isEmpty(selection)) {//若selection字段为空,则添加该字段 + String selectString = id > 0 ? parseSelection(selection) : selection; + for (String args : selectionArgs) { + selectString = selectString.replaceFirst("\\?", args); + } + sql.append(selectString);//增加selectString语句 + } + + mHelper.getWritableDatabase().execSQL(sql.toString());//把修改后的sql加入数据库 + } + + @Override + public String getType(Uri uri) {//获取uri的类型 + // TODO Auto-generated method stub + return null;//初始化,返回一个空指针 + } + +} From cab69bd9ca10c091819f6799ea21e87aa7ba19f5 Mon Sep 17 00:00:00 2001 From: wpx <2143423633@qq.com> Date: Tue, 11 Apr 2023 20:35:41 +0800 Subject: [PATCH 2/7] 4.11 --- .../GTaskClient.java | 585 ------------------ 1 file changed, 585 deletions(-) delete mode 100644 doc/王培鑫小米便签精读部分/GTaskClient.java diff --git a/doc/王培鑫小米便签精读部分/GTaskClient.java b/doc/王培鑫小米便签精读部分/GTaskClient.java deleted file mode 100644 index c67dfdf..0000000 --- a/doc/王培鑫小米便签精读部分/GTaskClient.java +++ /dev/null @@ -1,585 +0,0 @@ -/* - * 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.gtask.remote; - -import android.accounts.Account; -import android.accounts.AccountManager; -import android.accounts.AccountManagerFuture; -import android.app.Activity; -import android.os.Bundle; -import android.text.TextUtils; -import android.util.Log; - -import net.micode.notes.gtask.data.Node; -import net.micode.notes.gtask.data.Task; -import net.micode.notes.gtask.data.TaskList; -import net.micode.notes.gtask.exception.ActionFailureException; -import net.micode.notes.gtask.exception.NetworkFailureException; -import net.micode.notes.tool.GTaskStringUtils; -import net.micode.notes.ui.NotesPreferenceActivity; - -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.cookie.Cookie; -import org.apache.http.impl.client.BasicCookieStore; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.message.BasicNameValuePair; -import org.apache.http.params.BasicHttpParams; -import org.apache.http.params.HttpConnectionParams; -import org.apache.http.params.HttpParams; -import org.apache.http.params.HttpProtocolParams; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.LinkedList; -import java.util.List; -import java.util.zip.GZIPInputStream; -import java.util.zip.Inflater; -import java.util.zip.InflaterInputStream; - - -public class GTaskClient { - private static final String TAG = GTaskClient.class.getSimpleName(); - - private static final String GTASK_URL = "https://mail.google.com/tasks/"; - - private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig"; - - private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig"; - - private static GTaskClient mInstance = null; - - private DefaultHttpClient mHttpClient; - - private String mGetUrl; - - private String mPostUrl; - - private long mClientVersion; - - private boolean mLoggedin; - - private long mLastLoginTime; - - private int mActionId; - - private Account mAccount; - - private JSONArray mUpdateArray; - - private GTaskClient() { - mHttpClient = null; - mGetUrl = GTASK_GET_URL; - mPostUrl = GTASK_POST_URL; - mClientVersion = -1; - mLoggedin = false; - mLastLoginTime = 0; - mActionId = 1; - mAccount = null; - mUpdateArray = null; - } - - public static synchronized GTaskClient getInstance() { - if (mInstance == null) { - mInstance = new GTaskClient(); - } - return mInstance; - } - - public boolean login(Activity activity) { - // we suppose that the cookie would expire after 5 minutes - // then we need to re-login - final long interval = 1000 * 60 * 5; - if (mLastLoginTime + interval < System.currentTimeMillis()) { - mLoggedin = false; - } - - // need to re-login after account switch - if (mLoggedin - && !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity - .getSyncAccountName(activity))) { - mLoggedin = false; - } - - if (mLoggedin) { - Log.d(TAG, "already logged in"); - return true; - } - - mLastLoginTime = System.currentTimeMillis(); - String authToken = loginGoogleAccount(activity, false); - if (authToken == null) { - Log.e(TAG, "login google account failed"); - return false; - } - - // login with custom domain if necessary - if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase() - .endsWith("googlemail.com"))) { - StringBuilder url = new StringBuilder(GTASK_URL).append("a/"); - int index = mAccount.name.indexOf('@') + 1; - String suffix = mAccount.name.substring(index); - url.append(suffix + "/"); - mGetUrl = url.toString() + "ig"; - mPostUrl = url.toString() + "r/ig"; - - if (tryToLoginGtask(activity, authToken)) { - mLoggedin = true; - } - } - - // try to login with google official url - if (!mLoggedin) { - mGetUrl = GTASK_GET_URL; - mPostUrl = GTASK_POST_URL; - if (!tryToLoginGtask(activity, authToken)) { - return false; - } - } - - mLoggedin = true; - return true; - } - - private String loginGoogleAccount(Activity activity, boolean invalidateToken) { - String authToken; - AccountManager accountManager = AccountManager.get(activity); - Account[] accounts = accountManager.getAccountsByType("com.google"); - - if (accounts.length == 0) { - Log.e(TAG, "there is no available google account"); - return null; - } - - String accountName = NotesPreferenceActivity.getSyncAccountName(activity); - Account account = null; - for (Account a : accounts) { - if (a.name.equals(accountName)) { - account = a; - break; - } - } - if (account != null) { - mAccount = account; - } else { - Log.e(TAG, "unable to get an account with the same name in the settings"); - return null; - } - - // get the token now - AccountManagerFuture accountManagerFuture = accountManager.getAuthToken(account, - "goanna_mobile", null, activity, null, null); - try { - Bundle authTokenBundle = accountManagerFuture.getResult(); - authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN); - if (invalidateToken) { - accountManager.invalidateAuthToken("com.google", authToken); - loginGoogleAccount(activity, false); - } - } catch (Exception e) { - Log.e(TAG, "get auth token failed"); - authToken = null; - } - - return authToken; - } - - private boolean tryToLoginGtask(Activity activity, String authToken) { - if (!loginGtask(authToken)) { - // maybe the auth token is out of date, now let's invalidate the - // token and try again - authToken = loginGoogleAccount(activity, true); - if (authToken == null) { - Log.e(TAG, "login google account failed"); - return false; - } - - if (!loginGtask(authToken)) { - Log.e(TAG, "login gtask failed"); - return false; - } - } - return true; - } - - private boolean loginGtask(String authToken) { - int timeoutConnection = 10000; - int timeoutSocket = 15000; - HttpParams httpParameters = new BasicHttpParams(); - HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); - HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); - mHttpClient = new DefaultHttpClient(httpParameters); - BasicCookieStore localBasicCookieStore = new BasicCookieStore(); - mHttpClient.setCookieStore(localBasicCookieStore); - HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false); - - // login gtask - try { - String loginUrl = mGetUrl + "?auth=" + authToken; - HttpGet httpGet = new HttpGet(loginUrl); - HttpResponse response = null; - response = mHttpClient.execute(httpGet); - - // get the cookie now - List cookies = mHttpClient.getCookieStore().getCookies(); - boolean hasAuthCookie = false; - for (Cookie cookie : cookies) { - if (cookie.getName().contains("GTL")) { - hasAuthCookie = true; - } - } - if (!hasAuthCookie) { - Log.w(TAG, "it seems that there is no auth cookie"); - } - - // get the client version - String resString = getResponseContent(response.getEntity()); - String jsBegin = "_setup("; - String jsEnd = ")}"; - int begin = resString.indexOf(jsBegin); - int end = resString.lastIndexOf(jsEnd); - String jsString = null; - if (begin != -1 && end != -1 && begin < end) { - jsString = resString.substring(begin + jsBegin.length(), end); - } - JSONObject js = new JSONObject(jsString); - mClientVersion = js.getLong("v"); - } catch (JSONException e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); - return false; - } catch (Exception e) { - // simply catch all exceptions - Log.e(TAG, "httpget gtask_url failed"); - return false; - } - - return true; - } - - private int getActionId() { - return mActionId++; - } - - private HttpPost createHttpPost() { - HttpPost httpPost = new HttpPost(mPostUrl); - httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); - httpPost.setHeader("AT", "1"); - return httpPost; - } - - private String getResponseContent(HttpEntity entity) throws IOException { - String contentEncoding = null; - if (entity.getContentEncoding() != null) { - contentEncoding = entity.getContentEncoding().getValue(); - Log.d(TAG, "encoding: " + contentEncoding); - } - - InputStream input = entity.getContent(); - if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) { - input = new GZIPInputStream(entity.getContent()); - } else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) { - Inflater inflater = new Inflater(true); - input = new InflaterInputStream(entity.getContent(), inflater); - } - - try { - InputStreamReader isr = new InputStreamReader(input); - BufferedReader br = new BufferedReader(isr); - StringBuilder sb = new StringBuilder(); - - while (true) { - String buff = br.readLine(); - if (buff == null) { - return sb.toString(); - } - sb = sb.append(buff); - } - } finally { - input.close(); - } - } - - private JSONObject postRequest(JSONObject js) throws NetworkFailureException { - if (!mLoggedin) { - Log.e(TAG, "please login first"); - throw new ActionFailureException("not logged in"); - } - - HttpPost httpPost = createHttpPost(); - try { - LinkedList list = new LinkedList(); - list.add(new BasicNameValuePair("r", js.toString())); - UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8"); - httpPost.setEntity(entity); - - // execute the post - HttpResponse response = mHttpClient.execute(httpPost); - String jsString = getResponseContent(response.getEntity()); - return new JSONObject(jsString); - - } catch (ClientProtocolException e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); - throw new NetworkFailureException("postRequest failed"); - } catch (IOException e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); - throw new NetworkFailureException("postRequest failed"); - } catch (JSONException e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); - throw new ActionFailureException("unable to convert response content to jsonobject"); - } catch (Exception e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); - throw new ActionFailureException("error occurs when posting request"); - } - } - - public void createTask(Task task) throws NetworkFailureException { - commitUpdate(); - try { - JSONObject jsPost = new JSONObject(); - JSONArray actionList = new JSONArray(); - - // action_list - actionList.put(task.getCreateAction(getActionId())); - jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); - - // client_version - jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); - - // post - JSONObject jsResponse = postRequest(jsPost); - JSONObject jsResult = (JSONObject) jsResponse.getJSONArray( - GTaskStringUtils.GTASK_JSON_RESULTS).get(0); - task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); - - } catch (JSONException e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); - throw new ActionFailureException("create task: handing jsonobject failed"); - } - } - - public void createTaskList(TaskList tasklist) throws NetworkFailureException { - commitUpdate(); - try { - JSONObject jsPost = new JSONObject(); - JSONArray actionList = new JSONArray(); - - // action_list - actionList.put(tasklist.getCreateAction(getActionId())); - jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); - - // client version - jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); - - // post - JSONObject jsResponse = postRequest(jsPost); - JSONObject jsResult = (JSONObject) jsResponse.getJSONArray( - GTaskStringUtils.GTASK_JSON_RESULTS).get(0); - tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); - - } catch (JSONException e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); - throw new ActionFailureException("create tasklist: handing jsonobject failed"); - } - } - - public void commitUpdate() throws NetworkFailureException { - if (mUpdateArray != null) { - try { - JSONObject jsPost = new JSONObject(); - - // action_list - jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray); - - // client_version - jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); - - postRequest(jsPost); - mUpdateArray = null; - } catch (JSONException e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); - throw new ActionFailureException("commit update: handing jsonobject failed"); - } - } - } - - public void addUpdateNode(Node node) throws NetworkFailureException { - if (node != null) { - // too many update items may result in an error - // set max to 10 items - if (mUpdateArray != null && mUpdateArray.length() > 10) { - commitUpdate(); - } - - if (mUpdateArray == null) - mUpdateArray = new JSONArray(); - mUpdateArray.put(node.getUpdateAction(getActionId())); - } - } - - public void moveTask(Task task, TaskList preParent, TaskList curParent) - throws NetworkFailureException { - commitUpdate(); - try { - JSONObject jsPost = new JSONObject(); - JSONArray actionList = new JSONArray(); - JSONObject action = new JSONObject(); - - // action_list - action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, - GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE); - action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); - action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid()); - if (preParent == curParent && task.getPriorSibling() != null) { - // put prioring_sibing_id only if moving within the tasklist and - // it is not the first one - action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling()); - } - action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid()); - action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid()); - if (preParent != curParent) { - // put the dest_list only if moving between tasklists - action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid()); - } - actionList.put(action); - jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); - - // client_version - jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); - - postRequest(jsPost); - - } catch (JSONException e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); - throw new ActionFailureException("move task: handing jsonobject failed"); - } - } - - public void deleteNode(Node node) throws NetworkFailureException { - commitUpdate(); - try { - JSONObject jsPost = new JSONObject(); - JSONArray actionList = new JSONArray(); - - // action_list - node.setDeleted(true); - actionList.put(node.getUpdateAction(getActionId())); - jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); - - // client_version - jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); - - postRequest(jsPost); - mUpdateArray = null; - } catch (JSONException e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); - throw new ActionFailureException("delete node: handing jsonobject failed"); - } - } - - public JSONArray getTaskLists() throws NetworkFailureException { - if (!mLoggedin) { - Log.e(TAG, "please login first"); - throw new ActionFailureException("not logged in"); - } - - try { - HttpGet httpGet = new HttpGet(mGetUrl); - HttpResponse response = null; - response = mHttpClient.execute(httpGet); - - // get the task list - String resString = getResponseContent(response.getEntity()); - String jsBegin = "_setup("; - String jsEnd = ")}"; - int begin = resString.indexOf(jsBegin); - int end = resString.lastIndexOf(jsEnd); - String jsString = null; - if (begin != -1 && end != -1 && begin < end) { - jsString = resString.substring(begin + jsBegin.length(), end); - } - JSONObject js = new JSONObject(jsString); - return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS); - } catch (ClientProtocolException e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); - throw new NetworkFailureException("gettasklists: httpget failed"); - } catch (IOException e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); - throw new NetworkFailureException("gettasklists: httpget failed"); - } catch (JSONException e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); - throw new ActionFailureException("get task lists: handing jasonobject failed"); - } - } - - public JSONArray getTaskList(String listGid) throws NetworkFailureException { - commitUpdate(); - try { - JSONObject jsPost = new JSONObject(); - JSONArray actionList = new JSONArray(); - JSONObject action = new JSONObject(); - - // action_list - action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, - GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL); - action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); - action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid); - action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false); - actionList.put(action); - jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); - - // client_version - jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); - - JSONObject jsResponse = postRequest(jsPost); - return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS); - } catch (JSONException e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); - throw new ActionFailureException("get task list: handing jsonobject failed"); - } - } - - public Account getSyncAccount() { - return mAccount; - } - - public void resetUpdateArray() { - mUpdateArray = null; - } -} From ac8a012470ddc54d5830f19a49642a66aef73bcb Mon Sep 17 00:00:00 2001 From: xsj <1642219697@qq.com> Date: Tue, 11 Apr 2023 20:46:27 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E5=B0=8F=E7=B1=B3=E4=BE=BF=E7=AD=BE?= =?UTF-8?q?=E7=B2=BE=E8=AF=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NoteWidgetProvider.java | 132 +++ .../NoteWidgetProvider_2x.java | 46 + .../NoteWidgetProvider_4x.java | 46 + .../NotesListActivity.java | 956 ++++++++++++++++++ .../NotesListAdapter.java | 184 ++++ .../NotesListItem.java | 122 +++ .../NotesPreferenceActivity.java | 388 +++++++ 7 files changed, 1874 insertions(+) create mode 100644 doc/熊诗婕的精读部分/NoteWidgetProvider.java create mode 100644 doc/熊诗婕的精读部分/NoteWidgetProvider_2x.java create mode 100644 doc/熊诗婕的精读部分/NoteWidgetProvider_4x.java create mode 100644 doc/熊诗婕的精读部分/NotesListActivity.java create mode 100644 doc/熊诗婕的精读部分/NotesListAdapter.java create mode 100644 doc/熊诗婕的精读部分/NotesListItem.java create mode 100644 doc/熊诗婕的精读部分/NotesPreferenceActivity.java diff --git a/doc/熊诗婕的精读部分/NoteWidgetProvider.java b/doc/熊诗婕的精读部分/NoteWidgetProvider.java new file mode 100644 index 0000000..d89f456 --- /dev/null +++ b/doc/熊诗婕的精读部分/NoteWidgetProvider.java @@ -0,0 +1,132 @@ +/* + * 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; //导入widget +import android.app.PendingIntent; //引入各种类 +import android.appwidget.AppWidgetManager; // 导入AppWidget +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; //引入android自动生成的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 { //构造了一个类继承Android原有的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"; //定义NoteWidgetProvider为标签TAG + //删除原有的context + @Override //@Override标识重写,供编译器验证 + public void onDeleted(Context context, int[] appWidgetIds) { //定义函数onDeleted用来进行删除相关操作 + ContentValues values = new ContentValues(); //创建contentvalues对象 + values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); //将当前NoteColumns类的ID存入数据库中 + for (int i = 0; i < appWidgetIds.length; i++) { + context.getContentResolver().update(Notes.CONTENT_NOTE_URI, //遍历并修改所有的URI值 + values, + NoteColumns.WIDGET_ID + "=?", + new String[] { String.valueOf(appWidgetIds[i])}); //valueOf() 方法用于返回给定参数的原生 Number 对象值 + } + } + + private Cursor getNoteWidgetInfo(Context context, int widgetId) { //获取窗口宽度信息 + return context.getContentResolver().query(Notes.CONTENT_NOTE_URI, //定义context寻找并返回获取的widget窗口信息 + PROJECTION, + NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?", //用ID来筛选,同时要是存在于未被删除的文件夹中 + new String[] { String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLER) }, + null); + } + //更新挂件信息 + protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { //用updata函数来实现小米便签挂件appWidgetlds信息的返回与更新 + update(context, appWidgetManager, appWidgetIds, false); //更新窗口部件 + } + + private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, //把更新的窗口ID保存到窗口管理器中 + boolean privacyMode) { + for (int i = 0; i < appWidgetIds.length; i++) { //每添加一个widget就会进行一次循环更新操作 + if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) { //若ID对应的窗口已经关闭,则跳过 + int bgId = ResourceParser.getDefaultBgId(context); //跳过那些已经关闭的窗口的ID + String snippet = ""; + Intent intent = new Intent(context, NoteEditActivity.class); //创建intent对象 + intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); //若目标在栈顶则跳转,否则新建 + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]); //通过使用Intent对象(用于Activity之间传递参数)来更新小米便签挂件的ID和类型的相关信息。 + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType()); //附加组件类型 + + Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]); //获取特定的对应对象 + if (c != null && c.moveToFirst()) { //处理多个窗口同位出现的情况 + if (c.getCount() > 1) { //cursor.getCount()返回cursor中的行数 + 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); //获取ID值 + intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID)); //目的 + intent.setAction(Intent.ACTION_VIEW); //为Intent设置一个动作Action + } else { //在Intent内设置一个Action + 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()); //获取AppWidget对应的视图 + 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( //获得一个PendingIntent,如果该意图要发生就相当于Context.startActivity(Intent) + context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); + } else { + rv.setTextViewText(R.id.widget_text, snippet); //设置 点击“按钮(widget_text)”时会触发的Intent,从而对按钮点击事件进行处理 + pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent, + PendingIntent.FLAG_UPDATE_CURRENT); + } + + rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent); //窗口服务部件 + appWidgetManager.updateAppWidget(appWidgetIds[i], rv); //调用集合管理器对集合进行更新 + } //其中public void updateAppWidget(int appWidgetIds, RemoteViews views)功能:以特定的views视图更新所有appWidgetIds的窗口小部件(AppWidget) 。同时会发送ACTION_APPWIDGET_UPDATE广播 + } + } + + protected abstract int getBgResourceId(int bgId); //从背景资源中获取当前应用ID + + protected abstract int getLayoutId(); //获取布局ID + + protected abstract int getWidgetType(); //获取挂件的类型,即2x2型或者4x4型 +} diff --git a/doc/熊诗婕的精读部分/NoteWidgetProvider_2x.java b/doc/熊诗婕的精读部分/NoteWidgetProvider_2x.java new file mode 100644 index 0000000..1868a5e --- /dev/null +++ b/doc/熊诗婕的精读部分/NoteWidgetProvider_2x.java @@ -0,0 +1,46 @@ +/* + * 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; //使用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类,定义了2*2大小的窗口,并对部分函数进行重载 + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { //更新窗口 + super.update(context, appWidgetManager, appWidgetIds); + } + //获取挂件信息 + @Override //返回2x2型小米便签挂件 + protected int getLayoutId() { //获取外部资源id + return R.layout.widget_2x; //获取窗口布局信息 + } + + @Override + protected int getBgResourceId(int bgId) { //获取背景id + return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); //返回了小米便签2x2型挂件的背景类型ID,即确定了2x2型的背景 + } + + @Override + protected int getWidgetType() { //确定窗口类型 + return Notes.TYPE_WIDGET_2X; // 宽度为2x + } +} diff --git a/doc/熊诗婕的精读部分/NoteWidgetProvider_4x.java b/doc/熊诗婕的精读部分/NoteWidgetProvider_4x.java new file mode 100644 index 0000000..81a44b6 --- /dev/null +++ b/doc/熊诗婕的精读部分/NoteWidgetProvider_4x.java @@ -0,0 +1,46 @@ +/* + * 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; + + //4x扩展 +public class NoteWidgetProvider_4x extends NoteWidgetProvider { //设置大小为4*4的小窗口 + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { //对更新窗口进行重载 + super.update(context, appWidgetManager, appWidgetIds); + } + //获取挂件信息 + protected int getLayoutId() { //返回窗口位置信息 + return R.layout.widget_4x; //返回了小米便签挂件是2x2型 + } + + @Override //由ID通过getBgResourceId函数得知相关信息 + protected int getBgResourceId(int bgId) { //获取背景颜色ID + return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); //返回了小米便签2x2型挂件的背景类型ID,即确定了2x2型的背景 + } + + @Override + protected int getWidgetType() { //返回窗口类型 + return Notes.TYPE_WIDGET_4X; //返回4x4的窗口类型 + } +} diff --git a/doc/熊诗婕的精读部分/NotesListActivity.java b/doc/熊诗婕的精读部分/NotesListActivity.java new file mode 100644 index 0000000..59f5c83 --- /dev/null +++ b/doc/熊诗婕的精读部分/NotesListActivity.java @@ -0,0 +1,956 @@ +/* + * 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.ui; //每个类都要进行package声明,声明它在哪个package内 + +import android.app.Activity; //引入了一系列实现这个类功能所必须的类 +import android.app.AlertDialog; +import android.app.Dialog; +import android.appwidget.AppWidgetManager; //APP的界宽 +import android.content.AsyncQueryHandler; +import android.content.ContentResolver; +import android.content.ContentValues; +import android.content.Context; //文本 +import android.content.DialogInterface; +import android.content.Intent; +import android.content.SharedPreferences; +import android.database.Cursor; //光标 +import android.os.AsyncTask; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.text.Editable; +import android.text.TextUtils; +import android.text.TextWatcher; //文本观察器 +import android.util.Log; +import android.view.ActionMode; +import android.view.ContextMenu; +import android.view.ContextMenu.ContextMenuInfo; //文本菜单 +import android.view.Display; +import android.view.HapticFeedbackConstants; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuItem; +import android.view.MenuItem.OnMenuItemClickListener; //菜单条款 +import android.view.MotionEvent; +import android.view.View; +import android.view.View.OnClickListener; //主界面 +import android.view.View.OnCreateContextMenuListener; +import android.view.View.OnTouchListener; +import android.view.inputmethod.InputMethodManager; //输入方法 +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.AdapterView.OnItemLongClickListener; //点击监听 +import android.widget.Button; +import android.widget.EditText; +import android.widget.ListView; +import android.widget.PopupMenu; +import android.widget.TextView; //文本视图 +import android.widget.Toast; + +import net.micode.notes.R; //Android自动生成的R类,在该类中根据不同的资源类型又生成了相应的内部类,该类包含了系统中使用到的所有资源文件的标示 +import net.micode.notes.data.Notes; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.gtask.remote.GTaskSyncService; //远程 +import net.micode.notes.model.WorkingNote; +import net.micode.notes.tool.BackupUtils; +import net.micode.notes.tool.DataUtils; +import net.micode.notes.tool.ResourceParser; +import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; +import net.micode.notes.widget.NoteWidgetProvider_2x; +import net.micode.notes.widget.NoteWidgetProvider_4x; + +import java.io.BufferedReader; +import java.io.IOException; //输入版本 +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.HashSet; + //小米便签主界面 +public class NotesListActivity extends Activity implements OnClickListener, OnItemLongClickListener { //extends 是继承父类,只要那个类不是声明为final或者那个类定义为abstract的就能继承;implements可以实现多个接口,用逗号分开 + private static final int FOLDER_NOTE_LIST_QUERY_TOKEN = 0; //声明并赋值一些不可更改的私有属性 + //文件夹中笔记列表疑问标记 + private static final int FOLDER_LIST_QUERY_TOKEN = 1; //查询记号 + //文件夹列表疑问标记 + private static final int MENU_FOLDER_DELETE = 0; //删除菜单文件 + // 阅读菜单文件 + private static final int MENU_FOLDER_VIEW = 1; //菜单中的查看文件夹对应的int值 + //阅读菜单文件 + private static final int MENU_FOLDER_CHANGE_NAME = 2; //更改菜单文件名称 + //更改菜单文件名称,长按文件夹出现的三个选项,查看,删除,改名 + private static final String PREFERENCE_ADD_INTRODUCTION = "net.micode.notes.introduction";//用于第一次打开小米便签的判断 + // 偏好增加说明 + private enum ListEditState { //列表编辑状态类 + NOTE_LIST, SUB_FOLDER, CALL_RECORD_FOLDER + }; + + private ListEditState mState; //声明一些私有属性 + //列表编辑状态 + private BackgroundQueryHandler mBackgroundQueryHandler; //实现后台疑问处理功能 + //后台疑问处理 + private NotesListAdapter mNotesListAdapter; //便签列表配适器 + //笔记列表适配器 + private ListView mNotesListView; //主界面的视图 + //笔记列表浏览 + private Button mAddNewNote; //最下方添加便签的按钮 + //新建便签 + private boolean mDispatch; //是否调度的判断变量 + //是否调度 + private int mOriginY; //首次触摸时屏幕上的垂直距离(y值) + // 起始 + private int mDispatchY; // 起始 + //分派 + private TextView mTitleBar; //子文件夹下标头 + //标题小节 + private long mCurrentFolderId;//当前文件夹的ID + // 当前文件的ID + private ContentResolver mContentResolver; //提供内容分析 + //内容分析器 + private ModeCallback mModeCallBack; //返回调用方法 + //调用返回方法 + private static final String TAG = "NotesListActivity"; //名称(可用于日志文件调试) + //标签名字 + public static final int NOTES_LISTVIEW_SCROLL_RATE = 30; //列表滚动速度 + //列表的滚动速度为30 + private NoteItemData mFocusNoteDataItem; //光标指向的物件的数据内容 + //聚焦笔记的数据项 + private static final String NORMAL_SELECTION = NoteColumns.PARENT_ID + "=?"; //定义私有字符串变量 + //表明处于非父文件夹的其他文件夹下 + private static final String ROOT_FOLDER_SELECTION = "(" + NoteColumns.TYPE + "<>" //用于表明处于父文件夹下(主列表) + + Notes.TYPE_SYSTEM + " AND " + NoteColumns.PARENT_ID + "=?)" + " OR (" + + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER + " AND " + + NoteColumns.NOTES_COUNT + ">0)"; + // 打开某个便签时,创建活动的请求码 + private final static int REQUEST_CODE_OPEN_NODE = 102; //请求代码开放节点 + private final static int REQUEST_CODE_NEW_NODE = 103; //请求代码新节点 + + @Override //功能为Activity生命周期开始时调用的函数,用来保存读取状态,设置界面 + protected void onCreate(Bundle savedInstanceState) { //创建类 + super.onCreate(savedInstanceState); //super相当于是指向当前对象的父类,可以用super.xxx来引用父类的成员 引用父类onCreate函数 + setContentView(R.layout.note_list); //设置内容的视图 + initResources(); //初始化资源 + + /** + * Insert an introduction when user firstly use this application + */ + setAppInfoFromRawRes(); //当第一次使用这个app的时候,插入介绍信息 + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { //对子模块的一些数据进行分析 + if (resultCode == RESULT_OK //结果值与要求值正确 + && (requestCode == REQUEST_CODE_OPEN_NODE || requestCode == REQUEST_CODE_NEW_NODE)) { + mNotesListAdapter.changeCursor(null); + } else { //super调用父类的protected函数,创建窗口时使用 + super.onActivityResult(requestCode, resultCode, data); //调用父类的方法 + } //super调用父类的protected函数,创建窗口时使用 + } + //利用原始资源文件设置APP的相关信息 + private void setAppInfoFromRawRes() { //通过原生资源设置APP信息 + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); //Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数 + if (!sp.getBoolean(PREFERENCE_ADD_INTRODUCTION, false)) { //添加新的偏好设置 + StringBuilder sb = new StringBuilder(); //读取原生资源信息 + InputStream in = null; //输入流初始设置为空 + try {//使用getResources获取资源后,以openRawResource方法打开这个文件 + in = getResources().openRawResource(R.raw.introduction); //加载Welcome to use MIUI notes!(本地xml文件) + if (in != null) { //如果加载到输入流成功,填充到缓冲区里 + InputStreamReader isr = new InputStreamReader(in); //使用指定的字符集读取字节并将它们解码为字符 + BufferedReader br = new BufferedReader(isr); //构建输入管道 + char [] buf = new char[1024]; + int len = 0; + while ((len = br.read(buf)) > 0) { //不断在buf中读取数据放入sb里 + sb.append(buf, 0, len); //使用append函数在指定元素的结尾插入内容 + } + } else { + Log.e(TAG, "Read introduction file error"); //报错,读取文件错误 + return; + } + } catch (IOException e) { //获取IO中断异常 + e.printStackTrace(); //打印栈轨迹 + return; + } finally { //finally是必然会执行的部分 + if(in != null) { + try { + in.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); //输出栈轨迹 + } + } + } + + WorkingNote note = WorkingNote.createEmptyNote(this, Notes.ID_ROOT_FOLDER, //新建笔记 + AppWidgetManager.INVALID_APPWIDGET_ID, Notes.TYPE_WIDGET_INVALIDE, + ResourceParser.RED); + note.setWorkingText(sb.toString()); // 设置文本数据 + if (note.saveNote()) { //这是一个判断语句,判断便签是否保存成功 + sp.edit().putBoolean(PREFERENCE_ADD_INTRODUCTION, true).commit(); //保存笔记 + } else { + Log.e(TAG, "Save introduction note error"); //如果便签保存不成功,则把错误信息打印到日志里面 + return; + } + } + } + + @Override + protected void onStart() { //在OnCreate函数运行完之后执行。用于获取便签列表 + super.onStart(); //调用父类,启动活动 + startAsyncNotesListQuery(); //同步列表中的便签信息 + } + + private void initResources() { //初始化资源 + mContentResolver = this.getContentResolver(); //声明一系列函数初始定义的私有变量 + mBackgroundQueryHandler = new BackgroundQueryHandler(this.getContentResolver()); //动态创建后台请求处理器的实例 + mCurrentFolderId = Notes.ID_ROOT_FOLDER; // 当前文件夹ID是根目录ID + mNotesListView = (ListView) findViewById(R.id.notes_list); //根据R文件中的id值查询到相应的View,然后返回 + mNotesListView.addFooterView(LayoutInflater.from(this).inflate(R.layout.note_list_footer, null), //使用布局填充器增加页脚视图 + null, false); + mNotesListView.setOnItemClickListener(new OnListItemClickListener()); //设置视图点击监听器 + mNotesListView.setOnItemLongClickListener(this); //设置长按监听器 + mNotesListAdapter = new NotesListAdapter(this); //创建便签视图配置器 + mNotesListView.setAdapter(mNotesListAdapter); //配置适配器 + mAddNewNote = (Button) findViewById(R.id.btn_new_note); //在activity中要获取该按钮 + mAddNewNote.setOnClickListener(this); //屏幕点击监视 + mAddNewNote.setOnTouchListener(new NewNoteOnTouchListener()); //长按监听器 + mDispatch = false; //是否调度,主要用于新建便签模块 + mDispatchY = 0;//初始y值设置为0 + mOriginY = 0; //加载文件夹下的标头资源 + mTitleBar = (TextView) findViewById(R.id.tv_title_bar); //设置Title bar,也就是app页面顶部的返回、选项、信息描述 + mState = ListEditState.NOTE_LIST; //设置状态为主界面 + mModeCallBack = new ModeCallback(); //继承自ListView.MultiChoiceModeListener 和 OnMenuItemClickListener + } + + private class ModeCallback implements ListView.MultiChoiceModeListener, OnMenuItemClickListener { // implements声明自己使用一个或多个接口 + private DropdownMenu mDropDownMenu; //下拉菜单 + private ActionMode mActionMode; //动作方式 + private MenuItem mMoveMenu;//移动菜单 + //创建动作方式,ActionMode是Android提供的一种创建菜单的方式 + public boolean onCreateActionMode(ActionMode mode, Menu menu) { //ActionMode 是 Android 提供的一种实现菜单方式 + getMenuInflater().inflate(R.menu.note_list_options, menu); //layout的xml布局文件实例化为View类对象 + menu.findItem(R.id.delete).setOnMenuItemClickListener(this); //这里关联上了listerner,专门关联在菜单的按键 + mMoveMenu = menu.findItem(R.id.move); //调用下面的更新菜单函数 + if (mFocusNoteDataItem.getParentId() == Notes.ID_CALL_RECORD_FOLDER //如果父类id在文件夹中保存或者用户文件数量为零,设置移动菜单为不可见,否者设为可见 + || DataUtils.getUserFolderCount(mContentResolver) == 0) { + mMoveMenu.setVisible(false); + } else { //设置菜单项目为可见 + mMoveMenu.setVisible(true); //当长按某一标签时,会执行这个,让 移动到文件夹 按键变得可见 + mMoveMenu.setOnMenuItemClickListener(this); //置菜单项监听器 + } + mActionMode = mode; + mNotesListAdapter.setChoiceMode(true); //进入选择模式 + mNotesListView.setLongClickable(false); //关闭长按列表项发生事件功能 + mAddNewNote.setVisibility(View.GONE); //隐藏了新增便签按钮 + + View customView = LayoutInflater.from(NotesListActivity.this).inflate( //设置用户界面 + R.layout.note_list_dropdown_menu, null); //加载下拉菜单的布局 + mode.setCustomView(customView); + mDropDownMenu = new DropdownMenu(NotesListActivity.this,//创建新的下拉菜单 + (Button) customView.findViewById(R.id.selection_menu), + R.menu.note_list_dropdown); //为view添加dropDownMenu(包含一个全选操作) + mDropDownMenu.setOnDropdownMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){ + public boolean onMenuItemClick(MenuItem item) { // 点击菜单时,设置为全选并更新菜单 + mNotesListAdapter.selectAll(!mNotesListAdapter.isAllSelected()); //对应下拉菜单里的全选按键 + updateMenu(); //更新菜单 + return true; + } + + }); //更新下拉菜单 + return true; + } + + private void updateMenu() { //在多选便签/便签文件夹时调用,用于更新多选下拉菜单 + int selectedCount = mNotesListAdapter.getSelectedCount(); //获取被勾选的条目数量 + // Update dropdown menu + String format = getResources().getString(R.string.menu_select_title, selectedCount); // 从原始资源中读取信息更改下拉菜单内容 + mDropDownMenu.setTitle(format); //更改标题 + MenuItem item = mDropDownMenu.findItem(R.id.action_select_all); //通过判断是否全选选项 + if (item != null) { //当全选成功,则将“全选”菜单项改为“取消全选”菜单,否则仍保持“全选”菜单项 + if (mNotesListAdapter.isAllSelected()) { //若便签被全选,则给出全不选按钮, 否则给出全选按钮 + item.setChecked(true); //取消全选 + item.setTitle(R.string.menu_deselect_all); + } else { + item.setChecked(false); + item.setTitle(R.string.menu_select_all); + } + } + } + + public boolean onPrepareActionMode(ActionMode mode, Menu menu) { //准备动作模式 + // TODO Auto-generated method stub + return false; + } + + public boolean onActionItemClicked(ActionMode mode, MenuItem item) {//菜单动作触发标记 + // TODO Auto-generated method stub + return false; + } + + public void onDestroyActionMode(ActionMode mode) { //销毁动作模式,设置便签可见 + mNotesListAdapter.setChoiceMode(false); //设置笔记列表适配器选择方式 + mNotesListView.setLongClickable(true); //长按操作 + mAddNewNote.setVisibility(View.VISIBLE);//设置新建的笔记为可见 + } + + public void finishActionMode() { //结束动作模式 + mActionMode.finish(); //菜单勾选状态改变 + } + + public void onItemCheckedStateChanged(ActionMode mode, int position, long id, //勾选状态改变时,更改勾选标志,更新菜单 + boolean checked) { //点击菜单选项触发操作 + mNotesListAdapter.setCheckedItem(position, checked); //功能描述:改变勾选的状态 函数实现:以便签列表配适器,设置相应项的勾选或者取消勾选 参数描述:@mode 行为模式@position 操作的便签的索引值@id 操作的便签的id@checked 是否勾选 + updateMenu(); //更新菜单 + } + + public boolean onMenuItemClick(MenuItem item) { //判断菜单是否被点击 + if (mNotesListAdapter.getSelectedCount() == 0) { //判断菜单是否被点击 + Toast.makeText(NotesListActivity.this, getString(R.string.menu_select_none), + Toast.LENGTH_SHORT).show(); + return true; //.Toast-Android系统中一种消息框类型 + } + + switch (item.getItemId()) { //根据id号判断是删除还是移动 + case R.id.delete: //点击delete选项 + AlertDialog.Builder builder = new AlertDialog.Builder(NotesListActivity.this); //删除选中的便签的标题 + builder.setTitle(getString(R.string.alert_title_delete)); //设置“删除选中的便签”的title + builder.setIcon(android.R.drawable.ic_dialog_alert); //设置提醒删除的图片 + builder.setMessage(getString(R.string.alert_message_delete_notes, //设置警告对话框的图标 + mNotesListAdapter.getSelectedCount())); + builder.setPositiveButton(android.R.string.ok, //设置否定按钮 + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, + int which) { + batchDelete(); //执行批量删除操作 + } + }); + builder.setNegativeButton(android.R.string.cancel, null); //.取消的按键的视图 + builder.show(); + break; + case R.id.move: //点击move选项 + startQueryDestinationFolders(); //启动查询目标文件函数 + break; + default: //default,switch语句结束 + return false; + } + return true; + } + } + + private class NewNoteOnTouchListener implements OnTouchListener { //触摸便签监听器 + + public boolean onTouch(View v, MotionEvent event) { //新建便签的触摸事件的处理 + switch (event.getAction()) { //获取不同动作对应不同操作 + case MotionEvent.ACTION_DOWN: { //如果是创建新便签,通过计算调整界面大小 + Display display = getWindowManager().getDefaultDisplay(); + int screenHeight = display.getHeight(); //获取屏幕高度 + int newNoteViewHeight = mAddNewNote.getHeight(); //下面那个“新建标签”按钮的高度 + int start = screenHeight - newNoteViewHeight; //获取新增便签的高度 + int eventY = start + (int) event.getY(); //event.getY相当于点击到的地方的y值 + /** + * Minus TitleBar's height + */ + if (mState == ListEditState.SUB_FOLDER) { //减去标题栏的高度 + eventY -= mTitleBar.getHeight(); + start -= mTitleBar.getHeight(); + } + /** + * HACKME:When click the transparent part of "New Note" button, dispatch //点击某个区域 + * the event to the list view behind this button. The transparent part of + * "New Note" button could be expressed by formula y=-0.12x+94(Unit:pixel) + * and the line top of the button. The coordinate based on left of the "New + * Note" button. The 94 represents maximum height of the transparent part. //点击按键 + * Notice that, if the background of the button changes, the formula should + * also change. This is very bad, just for the UI designer's strong requirement. + */ + if (event.getY() < (event.getX() * (-0.12) + 94)) { //判断是否点击中了new note的区域 + View view = mNotesListView.getChildAt(mNotesListView.getChildCount() - 1 //最后得到最后一个元素的view(视图) + - mNotesListView.getFooterViewsCount()); // 减去页脚下元素布局数量 + if (view != null && view.getBottom() > start //如果不在新建便签的按钮上,重新调度响应按键 + && (view.getTop() < (start + 94))) { + mOriginY = (int) event.getY(); // 初始按下时的垂直距离 + mDispatchY = eventY; //调度时的垂直距离 + event.setLocation(event.getX(), mDispatchY); // 重新给触摸事件定位 + mDispatch = true; //触摸事件定位 + return mNotesListView.dispatchTouchEvent(event); //重新调度,即重新执行 + } + } + break; + } + case MotionEvent.ACTION_MOVE: { //如果是移动操作,调度动作顺序 + if (mDispatch) { + mDispatchY += (int) event.getY() - mOriginY; // 移动后,触摸事件位置发生变换 + event.setLocation(event.getX(), mDispatchY); //移动后,触摸事件位置发生变换 + return mNotesListView.dispatchTouchEvent(event); //重新调度,即重新执行 + + } + break; + } + default: { //其他情况 + if (mDispatch) { + event.setLocation(event.getX(), mDispatchY); //重新赋值 + mDispatch = false; + return mNotesListView.dispatchTouchEvent(event); //重新调度,即重新执行 + } + break; + } + } + return false; + } + + }; + + private void startAsyncNotesListQuery() { //同步便签列表请求 + String selection = (mCurrentFolderId == Notes.ID_ROOT_FOLDER) ? ROOT_FOLDER_SELECTION //如果当前文件id与保存在文件夹的id相同,selection为文件夹模式,否则为常规模式 + : NORMAL_SELECTION; + mBackgroundQueryHandler.startQuery(FOLDER_NOTE_LIST_QUERY_TOKEN, null, //后台异步对数据库进行操作,加快数据处理速度 + Notes.CONTENT_NOTE_URI, NoteItemData.PROJECTION, selection, new String[] { + String.valueOf(mCurrentFolderId) + }, NoteColumns.TYPE + " DESC," + NoteColumns.MODIFIED_DATE + " DESC"); + } + //AsyncQueryHandler异步查询操作帮助类,可以处理增删改ContentProvider提供的数据。BackgroundQueryHandler继承这个类,可以实现对数据库的查询操作 + private final class BackgroundQueryHandler extends AsyncQueryHandler { //背景请求处理器 + public BackgroundQueryHandler(ContentResolver contentResolver) { //调用父类的方法 + super(contentResolver); + } + + @Override //查询完成后对光标的处理 + protected void onQueryComplete(int token, Object cookie, Cursor cursor) { //查询完成后对光标的处理 + switch (token) { + case FOLDER_NOTE_LIST_QUERY_TOKEN: //便签列表请求 + mNotesListAdapter.changeCursor(cursor); //如果是便签查询被采用,更改光标位置 + break; + case FOLDER_LIST_QUERY_TOKEN: //获取文件夹请求 + if (cursor != null && cursor.getCount() > 0) { // 如果下面有便签,打开文件夹列表弹窗 + showFolderListMenu(cursor); //显示文件夹目录 + } else { + Log.e(TAG, "Query folder failed"); //写入错误异常日志 + } + break; + default: + return; + } + } + } + + private void showFolderListMenu(Cursor cursor) { //执行批量删除的操作 + AlertDialog.Builder builder = new AlertDialog.Builder(NotesListActivity.this); //声明一个警告对话框 + builder.setTitle(R.string.menu_title_select_folder); //对话框的title是“选择文件夹” + final FoldersListAdapter adapter = new FoldersListAdapter(this, cursor); //文件夹列表配适器 + builder.setAdapter(adapter, new DialogInterface.OnClickListener() { //为对话框设置监听事件 + //处理点击事件 + public void onClick(DialogInterface dialog, int which) { //点击事件的处理方法 + DataUtils.batchMoveToFolder(mContentResolver, //移动到文件夹下 + mNotesListAdapter.getSelectedItemIds(), adapter.getItemId(which)); + Toast.makeText( + NotesListActivity.this, + getString(R.string.format_move_notes_to_folder, // 移动便签进入文件夹 + mNotesListAdapter.getSelectedCount(), + adapter.getFolderName(NotesListActivity.this, which)), + Toast.LENGTH_SHORT).show(); + mModeCallBack.finishActionMode(); //批量移动操作完成 + } + }); + builder.show(); //显示对话框 + } + //作用:创建新的便签 实现:创建Intent后,传入当前文件夹ID,并跳转到便器编辑视图 参数:无 + + private void createNewNote() { //创建新便签 + Intent intent = new Intent(this, NoteEditActivity.class); //新建一个意图,与NoteEditActivity相关联 + intent.setAction(Intent.ACTION_INSERT_OR_EDIT); // intent的隐式调用。ACTION_INSERT_OR_EDIT选择一个新条目或插入一个新条目去编辑它。setAction即为寻找能响应这个action的activity + intent.putExtra(Notes.INTENT_EXTRA_FOLDER_ID, mCurrentFolderId); //设置键对值 + this.startActivityForResult(intent, REQUEST_CODE_NEW_NODE); //这个活动发出请求,等待下一个活动返回数据 + } + + private void batchDelete() { //批量删除便签 + new AsyncTask>() { //功能描述: 批量删除:删除时候,会判断是否为桌面挂件 函数实现:调用DataUtils的batchDeleteNotes + protected HashSet doInBackground(Void... unused) { + HashSet widgets = mNotesListAdapter.getSelectedWidget(); + if (!isSyncMode()) { //异步处理任务 + // if not synced, delete notes directly + if (DataUtils.batchDeleteNotes(mContentResolver, mNotesListAdapter + .getSelectedItemIds())) { + } else { //如果不是同步模式则将笔记移到垃圾文件夹,若转移失败打印错误信息 + Log.e(TAG, "Delete notes error, should not happens"); //处于同步模式则将删除的便签移入回收文件夹 + } + } else { + // in sync mode, we'll move the deleted note into the trash //如果在同步模式,会将便签移动到“垃圾箱”文件夹 + // folder + if (!DataUtils.batchMoveToFolder(mContentResolver, mNotesListAdapter //同步状态先放入垃圾箱 + .getSelectedItemIds(), Notes.ID_TRASH_FOLER)) { //同步状态先放入回收站 + Log.e(TAG, "Move notes to trash folder error, should not happens"); + } + } + return widgets; //该方法在删除时判断便签是否有桌面挂件,若有则返回 + } + + @Override + protected void onPostExecute(HashSet widgets) { //这是一个循环体结构,如果id不等的话,会进行更新id的操作 + if (widgets != null) { + for (AppWidgetAttribute widget : widgets) { + if (widget.widgetId != AppWidgetManager.INVALID_APPWIDGET_ID // 此处判断是否为一个widget + && widget.widgetType != Notes.TYPE_WIDGET_INVALIDE) { //更新widget信息 + updateWidget(widget.widgetId, widget.widgetType); //更新桌面挂件 + } + } + } //结束动作 + mModeCallBack.finishActionMode(); //结束动作 + } + }.execute(); + } + + private void deleteFolder(long folderId) { //删除文件夹 + if (folderId == Notes.ID_ROOT_FOLDER) { //不在列表里,输出错误信息 + Log.e(TAG, "Wrong folder id, should not happen " + folderId); + return; + } + + HashSet ids = new HashSet(); //下面会判断删除的文件夹里是否包含与桌面挂件相关联的便签 + ids.add(folderId); //把文件夹id加入进去 + HashSet widgets = DataUtils.getFolderNoteWidget(mContentResolver, + folderId); + if (!isSyncMode()) { //如果不同步,直接删除 + // if not synced, delete folder directly + DataUtils.batchDeleteNotes(mContentResolver, ids); //如同步,将被删除的文件夹放入trash文件夹中 + } else { //否则放到回收站 + // in sync mode, we'll move the deleted folder into the trash folder //处于同步模式则放到“垃圾桶”文件夹里 + DataUtils.batchMoveToFolder(mContentResolver, ids, Notes.ID_TRASH_FOLER); //处于同步模式则放到回收站 + } + if (widgets != null) {//如果存在对应桌面挂件,那么判断挂件信息,如果挂件id有效且挂件类型有效,则更新挂件信息 + for (AppWidgetAttribute widget : widgets) { + if (widget.widgetId != AppWidgetManager.INVALID_APPWIDGET_ID + && widget.widgetType != Notes.TYPE_WIDGET_INVALIDE) { + updateWidget(widget.widgetId, widget.widgetType); + } + } + } + } + //作用:打开便签编辑界面 实现:创建一个新的Intent,绑定便签编辑界面NoteEditActivity,传入便签数据NoteItemData之后,设置跳转行动,跳转到便签编辑界面 参数:@data: 便签数据,可以看做便签数据的结构体 @return: 无 + private void openNode(NoteItemData data) { //打开便签 + Intent intent = new Intent(this, NoteEditActivity.class); //功能描述:打开便签,创建新的活动,并且等待返回值 函数实现:用intent传递数据 参数描述:@data 要打开的便签的数据项 + intent.setAction(Intent.ACTION_VIEW); + intent.putExtra(Intent.EXTRA_UID, data.getId()); //目的数据 + this.startActivityForResult(intent, REQUEST_CODE_OPEN_NODE); + } + //打开文件夹,进行数据处理 + private void openFolder(NoteItemData data) { //打开文件夹 + mCurrentFolderId = data.getId(); //获取当前文件夹的ID + startAsyncNotesListQuery(); //开始异步的便签列表反馈 + if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) { //对状态的操作 + mState = ListEditState.CALL_RECORD_FOLDER; //对标题栏的操作 + mAddNewNote.setVisibility(View.GONE); //将button“新建便签”置为不可见 + } else { //不然状态设置为子文件夹 + mState = ListEditState.SUB_FOLDER; + } + if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) { //如果当前id是保存在文件夹的id,设置标题内容为文件夹名字,否则设置为文本的前部片段内容 + mTitleBar.setText(R.string.call_record_folder_name); //title设置为call notes + } else { + mTitleBar.setText(data.getSnippet()); //否则文本的前部片段内容,即文件夹名称 + } + mTitleBar.setVisibility(View.VISIBLE); //将Activity的title设置为可见 + } + //作用:当点击“写便签”按钮时,打开新建便签界面 实现:判断点击的按钮的ID,如果是创建新便签按钮,执行函数createNewNote(),跳转到创建新便签界面 参数:@v:类型为View的变量,代表传入的视图 @return:无 + public void onClick(View v) { //点击时进行的响应 + switch (v.getId()) { //得到我们所点击组件的ID号并进行判断 + case R.id.btn_new_note: //当点击的组件是btn_new_note的时候创建一个新的标签 + createNewNote(); //当点击的组件是btn_new_note的时候创建一个新的标签 + break; + default: + break; + } + } + + private void showSoftInput() { //显示软键盘 + InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); //获取方法的管理者对象 + if (inputMethodManager != null) { //若输入为空,进行对应操作 + inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); + }//使软键盘显示,第二个参数hideFlags(用来设置是否隐藏)等于0 + } + + private void hideSoftInput(View view) { //关闭键盘 + InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); + } //隐藏软键盘 + // 显示创建或者修改文件夹的对话框 + private void showCreateOrModifyFolderDialog(final boolean create) { //创建或修改文件夹对话框 + final AlertDialog.Builder builder = new AlertDialog.Builder(this); //初始化对话框 + View view = LayoutInflater.from(this).inflate(R.layout.dialog_edit_text, null); //加载布局文件dialog_edit_text.xml + final EditText etName = (EditText) view.findViewById(R.id.et_foler_name); //获得et_foler_name这个组件并将其转化为EditText类 + showSoftInput(); //显示键盘 + if (!create) { //如果create==false将对话框标题设置为 修改文件夹名称,否则设置为新建文件夹 + if (mFocusNoteDataItem != null) { //关注项不为空 + etName.setText(mFocusNoteDataItem.getSnippet()); //获取片段来设置文档 + builder.setTitle(getString(R.string.menu_folder_change_name)); //通过构建器设置标题 + } else { //显示此次点击显示为空 + Log.e(TAG, "The long click data item is null"); //载入标签tag,报错 + return; + } + } else { //创建操作 + etName.setText(""); //否则设置空文本 + builder.setTitle(this.getString(R.string.menu_create_folder)); //设置“创建文件”的标题 + } + //设置对话框窗口的确定、取消键。 + builder.setPositiveButton(android.R.string.ok, null); //对话框设置确定和取消按钮 + builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { //设置加载失败的按钮 + public void onClick(DialogInterface dialog, int which) { //点击触发软键盘隐藏 + hideSoftInput(etName); //隐藏输入 + } + }); + + final Dialog dialog = builder.setView(view).show(); //将上述对话框实例化并显示在屏幕上 + final Button positive = (Button)dialog.findViewById(android.R.id.button1); //创建对话框的确认按钮 + positive.setOnClickListener(new OnClickListener() { //设置点击监听器 + public void onClick(View v) { //新建文件夹或者修改文件夹名同时判断其是否合法 + hideSoftInput(etName); //隐藏键盘 + String name = etName.getText().toString(); //获取当前可编辑文本框etName的文本内容 + if (DataUtils.checkVisibleFolderName(mContentResolver, name)) { //检测可视文件夹的名字 + Toast.makeText(NotesListActivity.this, getString(R.string.folder_exist, name), //创建文本 + Toast.LENGTH_LONG).show(); + etName.setSelection(0, etName.length()); //全选当前字符串 + return; + } + if (!create) { //当文件名字存在时,判断是否为创建操作,若不是,则更新values信息,若是,则插入values信息 + if (!TextUtils.isEmpty(name)) { //判断输入是不是空 + ContentValues values = new ContentValues(); //与Hashtable类似,负责存储名值对,名为string类型,值为基本类型 + values.put(NoteColumns.SNIPPET, name); //将片段名字添加入value + values.put(NoteColumns.TYPE, Notes.TYPE_FOLDER); //将文件夹类型添加入value + values.put(NoteColumns.LOCAL_MODIFIED, 1); //将本地修改添加入value + mContentResolver.update(Notes.CONTENT_NOTE_URI, values, NoteColumns.ID //将内容笔记URI及笔记专栏ID更新至内容解决器 + + "=?", new String[] { + String.valueOf(mFocusNoteDataItem.getId()) //如果修改文件夹名字操作成功,则更新数据库内容 + }); //new一个内容值 + } + } else if (!TextUtils.isEmpty(name)) { //若为创建文件夹,则向数据库插入数据 + ContentValues values = new ContentValues(); + values.put(NoteColumns.SNIPPET, name); //将SNIPPET加入内容值中 + values.put(NoteColumns.TYPE, Notes.TYPE_FOLDER); //将TYPE加入内容值中 + mContentResolver.insert(Notes.CONTENT_NOTE_URI, values); //将新建的文件夹插入到数据库中 + } + dialog.dismiss(); //撤销对话框 + } + }); + // 若文件不存在设置确定按键为不可用 + if (TextUtils.isEmpty(etName.getText())) { //etName为空设置按键不可用 + positive.setEnabled(false); //右下的button“ok”不可用,即显示灰色不能点击 + } + /** + * When the name edit text is null, disable the positive button + */ + etName.addTextChangedListener(new TextWatcher() { //添加文本改变监听器 + public void beforeTextChanged(CharSequence s, int start, int count, int after) { //判断是否在文本更改之前 + // TODO Auto-generated method stub //判断是否在文本更改之前 + + } + + public void onTextChanged(CharSequence s, int start, int before, int count) { //当前文本改变触发,文本为空按键不可用,不为空则可用 + if (TextUtils.isEmpty(etName.getText())) { //如果文件夹名称为空,那么便不可用 + positive.setEnabled(false); + } else { + positive.setEnabled(true); //监听输入字符串,如果大于零,则button可以点击 + } + } + + public void afterTextChanged(Editable s) { //判断是否在文本更改之后 + // TODO Auto-generated method stub + + } + }); + } + //作用:响应返回键的点击 实现:判断当前在哪类文件夹下面,如在子文件夹下面,就切换文件夹ID和状态,调用服务,同步存在的便签 如在首文件夹下面,就调用父类方法,直接返回桌面 参数:无 + @Override //按返回键时根据情况更改类中数据 + public void onBackPressed() { //点击后退键时的操作 + switch (mState) { //判断目前所处的状态 + case SUB_FOLDER: //前两个状态返回到初始界面最后一个状态返回到桌面 + mCurrentFolderId = Notes.ID_ROOT_FOLDER; //如果是在文件夹中的状态 + mState = ListEditState.NOTE_LIST; ///将当前文件夹id修改为根文件夹 + startAsyncNotesListQuery(); //将当前状态改为一般状态 + mTitleBar.setVisibility(View.GONE); //隐藏TitleBar,设置为不可见 + break; + case CALL_RECORD_FOLDER: //响应已记录文件夹的操作 + mCurrentFolderId = Notes.ID_ROOT_FOLDER; //将当前文件夹id修改为根文件夹 + mState = ListEditState.NOTE_LIST; //便签列表的返回 + mAddNewNote.setVisibility(View.VISIBLE); //设置button“新增便签”可见 + mTitleBar.setVisibility(View.GONE); //隐藏TitleBar,设置为不可见 + startAsyncNotesListQuery(); + break; + case NOTE_LIST: //从主界面退出 + super.onBackPressed(); //调用原来的onBackPressed方法 + break; + default: //其他情况直接break + break; + } + } + //更新不同widget的插件 + private void updateWidget(int appWidgetId, int appWidgetType) { //更新窗口插件 + Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE); //初始化部件大小管理 + if (appWidgetType == Notes.TYPE_WIDGET_2X) { //下面是判断不同类型widget的操作并建立不同的类型 + intent.setClass(this, NoteWidgetProvider_2x.class); //将便签插件提供器的类型设置成内容的类型 + } else if (appWidgetType == Notes.TYPE_WIDGET_4X) { //判断不同类型widget的操作并建立不同的类型 + intent.setClass(this, NoteWidgetProvider_4x.class); + } else { + Log.e(TAG, "Unspported widget type"); //否则显示“不支持的插件类型 + return; //返回错误日志信息 + } //添加额外的插件 + + intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] { //putExtra中两个参数为键对值,第一个参数为键名 + appWidgetId + }); //发送广播 + + sendBroadcast(intent); //设置成功的结果 + setResult(RESULT_OK, intent); //返回给上一个活动数据 + } + //声明监听器,建立菜单,包括名称,视图,删除操作,更改名称操作 + private final OnCreateContextMenuListener mFolderOnCreateContextMenuListener = new OnCreateContextMenuListener() { //对文件夹进行操作的菜单 + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { //创建目录的视图 + if (mFocusNoteDataItem != null) { //当笔记数据项不为空时,设置菜单 + menu.setHeaderTitle(mFocusNoteDataItem.getSnippet()); //设置菜单的题目是当前文件夹名字 + menu.add(0, MENU_FOLDER_VIEW, 0, R.string.menu_folder_view); //在菜单中新增项目“查看文件夹” + menu.add(0, MENU_FOLDER_DELETE, 0, R.string.menu_folder_delete); //在菜单中新增项目“删除文件夹” + menu.add(0, MENU_FOLDER_CHANGE_NAME, 0, R.string.menu_folder_change_name); //在菜单中新增项目“修改文件夹名称” + } + } + }; + + @Override //作用:关闭长按文件夹显示的菜单 实现:将长按文件夹的事件监听器置为空,后调用父类方法,关闭长按文件夹菜单 参数: @menu:长按文件夹显示的菜单 @return:无 + public void onContextMenuClosed(Menu menu) { //关闭菜单 + if (mNotesListView != null) { + mNotesListView.setOnCreateContextMenuListener(null); //不设置监听事件 + } + super.onContextMenuClosed(menu); + } + + @Override + public boolean onContextItemSelected(MenuItem item) { //对菜单项进行选择对应的相应 + if (mFocusNoteDataItem == null) { //若笔记数据项为空 + Log.e(TAG, "The long click data item is null"); //设置标签“长按数据项为空 + return false; + } + switch (item.getItemId()) { //通过获取项目ID,进行switch + case MENU_FOLDER_VIEW: //打开文件夹 + openFolder(mFocusNoteDataItem); + break; + case MENU_FOLDER_DELETE: //文件夹删除 + AlertDialog.Builder builder = new AlertDialog.Builder(this); //新建一个警告对话框,来警告是否确认删除文件夹 + builder.setTitle(getString(R.string.alert_title_delete)); //设置警告对话框题目为“删除” + builder.setIcon(android.R.drawable.ic_dialog_alert); //设置警告对话框图标为”三角感叹号“ + builder.setMessage(getString(R.string.alert_message_delete_folder)); //设置警告信息文本“确认删除文件夹及所包含的便签” + builder.setPositiveButton(android.R.string.ok, //设置取消按键 + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + deleteFolder(mFocusNoteDataItem.getId()); //点击删除文件夹 + } + }); //构建器_显示 + builder.setNegativeButton(android.R.string.cancel, null); //设置button“取消” + builder.show(); + break; + case MENU_FOLDER_CHANGE_NAME: //文件夹改名 + showCreateOrModifyFolderDialog(false); //不显示创建or修改文件夹的对话框 + break; + default: + break; + } + + return true; + } + + @Override //准备菜单选项,根据三个不同的state设置不同菜单选项 + public boolean onPrepareOptionsMenu(Menu menu) { //创建菜单这个方法在每一次调用菜单的时候都会执行 + menu.clear(); //菜单清空 + if (mState == ListEditState.NOTE_LIST) { //当状态为当前笔记列表状态时,设置同步或取消同步 + getMenuInflater().inflate(R.menu.note_list, menu); //加载目录的布局 + // set sync or sync_cancel //设置同步标题 + menu.findItem(R.id.menu_sync).setTitle( + GTaskSyncService.isSyncing() ? R.string.menu_sync_cancel : R.string.menu_sync); //为同步菜单项设置标题,如果正在同步中则显示“取消同步”,否则显示“同步” + } else if (mState == ListEditState.SUB_FOLDER) { //当状态为当前子文件夹状态时,扩充子文件夹 + getMenuInflater().inflate(R.menu.sub_folder, menu); //采用布局文件R.menu.sub_folder来构建菜单项 + } else if (mState == ListEditState.CALL_RECORD_FOLDER) { //当状态为当调用记录文件夹时,扩充记录文件夹的菜单 + getMenuInflater().inflate(R.menu.call_record_folder, menu); //采用布局文件R.menu.call_record_folder来构建菜单项 + } else { //否则设置标签为“错误状态”+当前状态 + Log.e(TAG, "Wrong state:" + mState); //报错日志信息 + } + return true; + } + //实现菜单项目的操作,通过case语句对各个菜单项目分别设置事件 + @Override //菜单选项被选择的响应 + public boolean onOptionsItemSelected(MenuItem item) { //当主界面中的菜单项被选中时进行的工作 (case R.id.menu_new_note)这个选项没有在菜单中显示出来 + switch (item.getItemId()) { //选择不同的项目名称 + case R.id.menu_new_folder: { //新建文件夹 + showCreateOrModifyFolderDialog(true); + break; + } + case R.id.menu_export_text: { //输出文本 + exportNoteToText(); + break; + } + case R.id.menu_sync: { //同步菜单 + if (isSyncMode()) { //同步 + if (TextUtils.equals(item.getTitle(), getString(R.string.menu_sync))) { //如果项目title与菜单同步相同,则进行同步 + GTaskSyncService.startSync(this); //利用到GTaskSyncService的类 + } else { //否则取消同步 + GTaskSyncService.cancelSync(this); + } + } else { //如果不是同步模式,则开始设置动作 + startPreferenceActivity(); //否则进行Preference活动 + } + break; + } + case R.id.menu_setting: { //设置菜单 + startPreferenceActivity(); + break; + } + case R.id.menu_new_note: { //新建便签 + createNewNote(); + break; + } + case R.id.menu_search: //搜索 + onSearchRequested(); //查询 + break; + default: + break; + } + return true; + } + //根据关键字搜索便签(还未实现) + @Override //搜索便签 + public boolean onSearchRequested() { //搜索便签 + startSearch(null, false, null /* appData */, false); + return true; + } + //实现将便签导出到文本功能 + private void exportNoteToText() { //将便签导出到文本 + final BackupUtils backup = BackupUtils.getInstance(NotesListActivity.this); //备份笔记信息 + new AsyncTask() { //异步任务 + + @Override + protected Integer doInBackground(Void... unused) { //未被占用的话后台进行 + return backup.exportToText(); + } + + @Override // 根据不同的情况,反馈给用户信息 + protected void onPostExecute(Integer result) { //设置备份的结果响应 + if (result == BackupUtils.STATE_SD_CARD_UNMOUONTED) { //根据结果为sd卡未装载、成功、系统错误三种进行处理,均是设置对话框、标题、信息以及确认按钮状态 + AlertDialog.Builder builder = new AlertDialog.Builder(NotesListActivity.this); + builder.setTitle(NotesListActivity.this + .getString(R.string.failed_sdcard_export)); + builder.setMessage(NotesListActivity.this + .getString(R.string.error_sdcard_unmounted)); + builder.setPositiveButton(android.R.string.ok, null); + builder.show(); + } else if (result == BackupUtils.STATE_SUCCESS) { //如果导出成功,则显示相应对话框 + AlertDialog.Builder builder = new AlertDialog.Builder(NotesListActivity.this); //如果导出成功,则显示成功 并且会显示“Export text file 《便签名称》 to SD 《目录名称》 directory” + builder.setTitle(NotesListActivity.this + .getString(R.string.success_sdcard_export)); + builder.setMessage(NotesListActivity.this.getString( + R.string.format_exported_file_location, backup + .getExportedTextFileName(), backup.getExportedTextFileDir())); //导出文本 + builder.setPositiveButton(android.R.string.ok, null); + builder.show(); + } else if (result == BackupUtils.STATE_SYSTEM_ERROR) { //如果系统错误,则显示 导出失败,请检查SD卡 + AlertDialog.Builder builder = new AlertDialog.Builder(NotesListActivity.this); + builder.setTitle(NotesListActivity.this // 设置标题 + .getString(R.string.failed_sdcard_export)); + builder.setMessage(NotesListActivity.this + .getString(R.string.error_sdcard_export)); + builder.setPositiveButton(android.R.string.ok, null); //设置响应按钮 + builder.show(); + } + } + + }.execute(); + } + // 判断是否处于同步模式 + private boolean isSyncMode() { //判断同步 + return NotesPreferenceActivity.getSyncAccountName(this).trim().length() > 0; //转到PreferenceActivity + } + // 跳转到PreferenceActivity界面 + private void startPreferenceActivity() { //跳转到设置界面 + Activity from = getParent() != null ? getParent() : this; //设置便签函数 + Intent intent = new Intent(from, NotesPreferenceActivity.class); + from.startActivityIfNeeded(intent, -1); //请求码为-1,表示此活动结束后不会通知原活动 + } + //监听列表点击监听器,接口是OnItemClickListener + private class OnListItemClickListener implements OnItemClickListener { //当短按标签列表项时的响应 + + public void onItemClick(AdapterView parent, View view, int position, long id) { // 项目被点击的响应 + if (view instanceof NotesListItem) { //判断view是否是NotesListItem的一个实例,如果是就获取他的项目信息装入item中 + NoteItemData item = ((NotesListItem) view).getItemData(); //判断view是否是NotesListItem的一个实例 是就获取他的项目信息装入item中 + if (mNotesListAdapter.isInChoiceMode()) { //如果列表适配器被选择并且项是便签类型的,则修改位置和状态信息 + if (item.getType() == Notes.TYPE_NOTE) { //如果点到的item是便签 + position = position - mNotesListView.getHeaderViewsCount(); //减去头部视图的元素项,得到列表的元素索引值 + mModeCallBack.onItemCheckedStateChanged(null, position, id, //改变对应索引的Item是否被选中的状态 + !mNotesListAdapter.isSelectedItem(position)); + } + return; + } + + switch (mState) { //区别情况进行处理 + case NOTE_LIST: //便签列表 + if (item.getType() == Notes.TYPE_FOLDER + || item.getType() == Notes.TYPE_SYSTEM) { + openFolder(item); //查询目标文件夹 + } else if (item.getType() == Notes.TYPE_NOTE) { + openNode(item); //打开便签 + } else { + Log.e(TAG, "Wrong note type in NOTE_LIST"); + } + break; + case SUB_FOLDER: + case CALL_RECORD_FOLDER: + if (item.getType() == Notes.TYPE_NOTE) { + openNode(item); + } else { + Log.e(TAG, "Wrong note type in SUB_FOLDER"); + } + break; + default: + break; + } + } + } + + } + //功能描述:按下移动便签后,开始查找所有的文件夹 函数实现:mBackgroundQueryHandler的startQuery方法 + private void startQueryDestinationFolders() { //查询目标文件 + String selection = NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>? AND " + NoteColumns.ID + "<>?"; //功能描述:按下移动便签后,开始查找所有的文件夹 函数实现:mBackgroundQueryHandler的startQuery方法 + selection = (mState == ListEditState.NOTE_LIST) ? selection: + "(" + selection + ") OR (" + NoteColumns.ID + "=" + Notes.ID_ROOT_FOLDER + ")"; + + mBackgroundQueryHandler.startQuery(FOLDER_LIST_QUERY_TOKEN, + null, + Notes.CONTENT_NOTE_URI, // 长按某一项时进行的操作如果长按的是便签,则通过ActionMode菜单实现 + FoldersListAdapter.PROJECTION, + selection, //长按某一项时进行的操作 如果长按的是便签,则通过ActionMode菜单实现;如果长按的是文件夹,则通过ContextMenu菜单实现 + new String[] { //新建字符列表 + String.valueOf(Notes.TYPE_FOLDER), + String.valueOf(Notes.ID_TRASH_FOLER), + String.valueOf(mCurrentFolderId) + }, + NoteColumns.MODIFIED_DATE + " DESC"); + } + //实现长按项目的点击事件。如果长按的是便签,则通过ActionMode菜单实现;如果长按的是文件夹,则通过ContextMenu菜单实现 + public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { // 如果长按的是便签,则通过ActionMode菜单实现;如果长按的是文件夹,则通过ContextMenu菜单实现 + if (view instanceof NotesListItem) { // 判断view是否是NotesListItem的一个实例,如果是就获取他的项目信息装入item中 + mFocusNoteDataItem = ((NotesListItem) view).getItemData(); //聚焦的Item对象 + if (mFocusNoteDataItem.getType() == Notes.TYPE_NOTE && !mNotesListAdapter.isInChoiceMode()) { //长按的对象是便签时的处理,通过ActionMode实现 + if (mNotesListView.startActionMode(mModeCallBack) != null) { + mModeCallBack.onItemCheckedStateChanged(null, position, id, true); //开始对单个便签进行操作 + mNotesListView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); //执行长按动作触发震动反馈 + } else { //动作执行失败,返回日志信息 + Log.e(TAG, "startActionMode fails"); + } + } else if (mFocusNoteDataItem.getType() == Notes.TYPE_FOLDER) { //如果长按的的项目是文件夹类型,则执行ContextMenu菜单的实现 + mNotesListView.setOnCreateContextMenuListener(mFolderOnCreateContextMenuListener); + } + } + return false; + } +} diff --git a/doc/熊诗婕的精读部分/NotesListAdapter.java b/doc/熊诗婕的精读部分/NotesListAdapter.java new file mode 100644 index 0000000..d9de9d4 --- /dev/null +++ b/doc/熊诗婕的精读部分/NotesListAdapter.java @@ -0,0 +1,184 @@ +/* + * 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.ui; //引入tools包 + +import android.content.Context; //第19到31行导入各种类 +import android.database.Cursor; // 光标 +import android.util.Log; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CursorAdapter; + +import net.micode.notes.data.Notes; + +import java.util.Collection; //实现便签的编辑 +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; + //直译为便签表连接器,继承了CursorAdapter,它为cursor和ListView提供了连接的桥梁。所以NotesListAdapter实现的是鼠标和编辑便签链接的桥梁 + //便签列表适配器,拓展功能为光标适配器 +public class NotesListAdapter extends CursorAdapter { //继承了CursorAdapter + private static final String TAG = "NotesListAdapter"; //设置标签字符常量 + private Context mContext; //便签上下文、环境 + private HashMap mSelectedIndex; //HashMap是一个散列表,储存键值对的映射关系 + private int mNotesCount; //便签数 + private boolean mChoiceMode; //选择模式标志 + //桌面widget的属性,其中包括编号和类型 + public static class AppWidgetAttribute { //widget的编号和类型 + public int widgetId; //初始化便签链接器 + public int widgetType; + }; //父类对象置空 + //功能描述:初始化NotesListAdapter实例, 函数实现:以便签列表的context初始化便签列表配适器 + public NotesListAdapter(Context context) { //初始化便签链接 + super(context, null); //功能描述:NoteListAdapter的构造函数 函数实现:继承父类函数;设置HashMap的map表,实现选择item与是否选择的键值对;设置context上下文;初始化note数量为0 + mSelectedIndex = new HashMap(); //新建HASH表 + mContext = context; //新建一个视图来存储光标所指向的数据 + mNotesCount = 0; //初始便签数为0 + } + //功能描述:返回一个NotesListItem对象 函数实现:调用对象的构造方法,动态分配空间 参数描述:@context 内容存储器@cursor 光标对象@parent 父视图@return view 视图对象 + @Override //通过新建一个视图来存储光标所指向的数据 + public View newView(Context context, Cursor cursor, ViewGroup parent) { + return new NotesListItem(context); + } + + @Override + public void bindView(View view, Context context, Cursor cursor) { //利用NotesListLtem类创建新布局 + if (view instanceof NotesListItem) { //创建便签列表的数据项 + NoteItemData itemData = new NoteItemData(context, cursor); //新建一个项目选项并且用bind跟将view和鼠标,内容,便签数据捆绑在一起 + ((NotesListItem) view).bind(context, itemData, mChoiceMode, //用光标指向的内容新建项目并将数据、项目、鼠标、视图捆绑起来 + isSelectedItem(cursor.getPosition())); + } //新建一个项目选项,然后用bind函数将视图跟鼠标,内容,便签相关数据捆绑在一起 + } + //设置勾选项 + public void setCheckedItem(final int position, final boolean checked) { //设置勾选框 + mSelectedIndex.put(position, checked); //根据定位和是否勾选设置下标 + notifyDataSetChanged(); //在修改后刷新activity + } + //判断单选按钮是否勾选 + public boolean isInChoiceMode() { //判断单选按钮是否勾选 + return mChoiceMode; //设置单项选项框 + } + //设置单项选项框 + public void setChoiceMode(boolean mode) { //重置下标,并根据参数mode设置选项 + mSelectedIndex.clear(); //清空勾选下表并根据当前mode设置 + mChoiceMode = mode; + } + //选择全部选项 + public void selectAll(boolean checked) { //设置全部勾选 + Cursor cursor = getCursor(); //获取光标位置 + for (int i = 0; i < getCount(); i++) { //遍历可用光标位置,如果光标移动且光标当前指向的便签项目类型为TYPE_NOTE则设置为勾选状态 + if (cursor.moveToPosition(i)) { //遍历所有位置并设置勾选标志 + if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { //处于便签状态 + setCheckedItem(i, checked); //将位置i标志为已勾选加入到 mSelectedIndex中 + } //遍历所有的光标可用的位置,再判断便签类型后勾选单项框 + } + } + } + //创建选择项的下标列表 + public HashSet getSelectedItemIds() { //建立选择项目的ID的HASH表 + HashSet itemSet = new HashSet(); //建立hash表 + for (Integer position : mSelectedIndex.keySet()) { //遍历所有的关键 + if (mSelectedIndex.get(position) == true) { //判断光标位置是否可用 + Long id = getItemId(position); + if (id == Notes.ID_ROOT_FOLDER) { //原文件不需添加 + Log.d(TAG, "Wrong item id, should not happen"); //错误Id,不可添加 + } else { //如果不是,则加入条目集合 + itemSet.add(id); //将该id加入到选项集合当中 + } //将id该下标假如选项集合中 + } + } + + return itemSet; //返回条目集合 + } + //功能描述:以哈希表形式返回选中的桌面挂件 函数实现:同getSelectedItemIds 参数描述:@return 桌面挂件的集合 + public HashSet getSelectedWidget() { //建立桌面widget选项表 + HashSet itemSet = new HashSet(); //类似于getselecteditemids的实现方法 + for (Integer position : mSelectedIndex.keySet()) { //遍历被选中的列表 + if (mSelectedIndex.get(position) == true) { + Cursor c = (Cursor) getItem(position); //用c记录光标位置以判断是否选择了桌面挂件(可用) + if (c != null) { //获取光标位置可用 + AppWidgetAttribute widget = new AppWidgetAttribute(); //新建widget并更新ID和类型,最后添加到选项表中 + NoteItemData item = new NoteItemData(mContext, c); //初始化所选桌面挂件信息加入到itemSet中 + widget.widgetId = item.getWidgetId(); + widget.widgetType = item.getWidgetType(); + itemSet.add(widget); //加入条目集合 + /** + * Don't close cursor here, only the adapter could close it + */ + } else { //在这里,不关闭光标,而是在adapter中才能关闭光标 + Log.e(TAG, "Invalid cursor"); //设置标签,无效的cursor + return null; + } + } + } + return itemSet; + } + //获取选项个数 + public int getSelectedCount() { //获取选项个数 + Collection values = mSelectedIndex.values(); //获取下标的值 + if (null == values) { //如果此项值为空贼返回0 + return 0; + } + Iterator iter = values.iterator(); //初始化迭代器 + int count = 0; + while (iter.hasNext()) { //循环计算选中便签的数目 + if (true == iter.next()) { //value值为真则count加一 + count++; + } + } + return count; + } + //判断是否全部选中 + public boolean isAllSelected() { //函数判断是否是全选 + int checkedCount = getSelectedCount(); //通过获得计数的结果与小米便签中的数量相比较 + return (checkedCount != 0 && checkedCount == mNotesCount); //对比选项数和总数是否一致且不为0 + } + //判断是否为选项表 + public boolean isSelectedItem(final int position) { //通过下标来判断是否为选项表 + if (null == mSelectedIndex.get(position)) { //如果下标为空则返回false + return false; + } + return mSelectedIndex.get(position); //判断Item是否被选中的状态 + } + + @Override + protected void onContentChanged() { //如果便签的内容发生改变就要重新计算一下 便签的数量 + super.onContentChanged(); //执行父类函数 + calcNotesCount(); + } + + @Override //在activity光标发生局部变化时回调该函数计算便签的数量 + public void changeCursor(Cursor cursor) { // 光标变动时重新计算便签数量 + super.changeCursor(cursor); //重载父类函数 + calcNotesCount(); //calcNotesCount函数实现 + } + // 计算便签数量 + private void calcNotesCount() { //实现方式类似前面代码中的selectAll函数 + mNotesCount = 0; + for (int i = 0; i < getCount(); i++) { //获取总数同时遍历 + Cursor c = (Cursor) getItem(i); //遍历所有选项 + if (c != null) { //判断语句,如果光标不是null,那么便得到信息,便签数目加1 + if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) { //若选项的数据类型为便签类型,那么计数+1 + mNotesCount++; + } + } else { //设置为无效的光标 + Log.e(TAG, "Invalid cursor"); //日志记录,将错误:无效光标事件记录到日志中去 + return; //否则就将设为无效光标 + } + } + } +} diff --git a/doc/熊诗婕的精读部分/NotesListItem.java b/doc/熊诗婕的精读部分/NotesListItem.java new file mode 100644 index 0000000..566cbd1 --- /dev/null +++ b/doc/熊诗婕的精读部分/NotesListItem.java @@ -0,0 +1,122 @@ +/* + * 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.ui; //引入需要用到的tool包 + +import android.content.Context; //导入各种类 +import android.text.format.DateUtils; +import android.view.View; //导入类 +import android.widget.CheckBox; +import android.widget.ImageView; +import android.widget.LinearLayout; //标签列表项目选项 +import android.widget.TextView; + +import net.micode.notes.R; +import net.micode.notes.data.Notes; +import net.micode.notes.tool.DataUtils; +import net.micode.notes.tool.ResourceParser.NoteItemBgResources; + // 引用包库 + //创建列表项目选项,拓展功能为线性布局 +public class NotesListItem extends LinearLayout { //构建便签列表的各个项目的详细具体信息 + private ImageView mAlert; //闹钟图片 + private TextView mTitle; //标题的文本视图 + private TextView mTime; //最后修改的时间的文本视图 + private TextView mCallName; //呼叫名称 + private NoteItemData mItemData; //标签数据 + private CheckBox mCheckBox; //勾选框 + //初始化 + public NotesListItem(Context context) { //初始化基本信息 + super(context); //调用父类具有相同形参的构造函数 + inflate(context, R.layout.note_item, this); //Inflate()作用就是将xml定义的一个布局找出来,但仅仅是找出来而且隐藏的,没有找到的同时并显示功能,这里的xml是R.layout + mAlert = (ImageView) findViewById(R.id.iv_alert_icon); //findViewById用于从contentView中查找指定ID的View,转换出来的形式根据需要而定 + mTitle = (TextView) findViewById(R.id.tv_title); //获取题目 + mTime = (TextView) findViewById(R.id.tv_time); //获取创建或修改时间 + mCallName = (TextView) findViewById(R.id.tv_name); //获取联系人姓名 + mCheckBox = (CheckBox) findViewById(android.R.id.checkbox); //获取复选框 + } //findViewById用于从contentView中查找指定ID的View,转换出来的形式根据需要而定 + + public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) { //根据data的属性对各个控件的属性的控制,主要是可见性Visibility,内容setText,格式setTextAppearance + if (choiceMode && data.getType() == Notes.TYPE_NOTE) { //如果处于选择模式并且是便签类型时,设置为可见及勾选,否则设置为不可见 + mCheckBox.setVisibility(View.VISIBLE); //设置View可见 + mCheckBox.setChecked(checked); // 设置当前项目被选中 + } else { + mCheckBox.setVisibility(View.GONE); //设置复选框不可见 + } + + mItemData = data; //把数据传给标签 + if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) { //设置控件属性,通过判断保存到文件夹的ID、当前ID以及父ID之间关系决定 + mCallName.setVisibility(View.GONE); //设置联系人名字不可见 + mAlert.setVisibility(View.VISIBLE); //设置闹钟图标可见 + mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); //设置外观风格 + mTitle.setText(context.getString(R.string.call_record_folder_name) //设置内容 + + context.getString(R.string.format_folder_files_count, data.getNotesCount())); + mAlert.setImageResource(R.drawable.call_record); //设置图片来源 + } else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) { //设置闹钟 + mCallName.setVisibility(View.VISIBLE); //设置联系人姓名可见 + mCallName.setText(data.getCallName()); //设置联系人姓名的文本内容 + mTitle.setTextAppearance(context,R.style.TextAppearanceSecondaryItem); //设置title文本风格 + mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); //设置title的文本内容为便签内容的前面片段 + if (data.hasAlert()) { // 如果时间提醒存在,设置图片来源,将时间提醒图标定为可见 + mAlert.setImageResource(R.drawable.clock); //图片来源的设置 + mAlert.setVisibility(View.VISIBLE); //将提醒图标设置为可见 + } else { + mAlert.setVisibility(View.GONE); //否则将提醒图标设置为不可见 + } //上面的设置均在ID_CALL_RECORD_FOLDER文件下 + } else { //如果父类和当前id均与保存在文件夹中的id不同 + mCallName.setVisibility(View.GONE);//设置联系人姓名不可见 + mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); //设置title的文本格式 + + if (data.getType() == Notes.TYPE_FOLDER) { //设置Type格式 + mTitle.setText(data.getSnippet() //设置便签标题内容为便签的前面部分的内容+文件数+便签数 + + context.getString(R.string.format_folder_files_count, //设置文件夹的title为“名字+(count)” + data.getNotesCount())); //设置时间,从data编辑的日期获取 + mAlert.setVisibility(View.GONE); //设置图标不可见 + } else { //设置便签的title为便签内容的前面片段 + mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); //如果是便签,则设置便签标题内容为便签的前面部分的内容 + if (data.hasAlert()) { //如果当前便签存在提醒闹钟时间 + mAlert.setImageResource(R.drawable.clock); //将提醒图标设置为闹钟样式 + mAlert.setVisibility(View.VISIBLE);//设置提醒闹钟可见 + } else { + mAlert.setVisibility(View.GONE);//否则设置提醒图标不可见 + } + } + } + mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate())); //将时间设置为编辑便签的时间 + //设置项目修改时间 + setBackground(data); //设置背景 + } + + private void setBackground(NoteItemData data) { //根据data的文件属性来设置背景 + int id = data.getBgColorId(); //获取id,用此id用来获取背景颜色 + if (data.getType() == Notes.TYPE_NOTE) { //若是note型文件,则4种情况,对于4种不同情况的背景来源 + if (data.isSingle() || data.isOneFollowingFolder()) { //单个数据或只有一个子文件夹 + setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id)); //单个数据或只有一个子文件夹 + } else if (data.isLast()) { //最后一个数据 + setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id)); //设置背景来源为id的最后一个数据 + } else if (data.isFirst() || data.isMultiFollowingFolder()) { //是第一个数据并有多个子文件夹 + setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id)); //若不是Note类型则使用文件夹背景来源 + } else { + setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id)); //将便签设置为普通类型便签的背景 + } + } else { //如果不是note直接调用文件夹的背景来源 + setBackgroundResource(NoteItemBgResources.getFolderBgRes()); // 设置背景来源为文件夹 + } + } + //返回当前便签的数据信息 + public NoteItemData getItemData() { // 返回当前便签的数据信息 + return mItemData; + } +} diff --git a/doc/熊诗婕的精读部分/NotesPreferenceActivity.java b/doc/熊诗婕的精读部分/NotesPreferenceActivity.java new file mode 100644 index 0000000..28ff175 --- /dev/null +++ b/doc/熊诗婕的精读部分/NotesPreferenceActivity.java @@ -0,0 +1,388 @@ +/* + * 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. + */ //该类功能:NotesPreferenceActivity,在小米便签中主要实现的是对背景颜色和字体大小的数据储存 + //声明许可证使用 +package net.micode.notes.ui; //引入ui包 + +import android.accounts.Account; //该类主要实现的是对背景颜色和字体大小的数据储存, 继承了PreferenceActivity主要功能为对系统信息和配置进行自动保存的Activity +import android.accounts.AccountManager; +import android.app.ActionBar; +import android.app.AlertDialog; +import android.content.BroadcastReceiver; +import android.content.ContentValues; +import android.content.Context; //文本 +import android.content.DialogInterface; //对话 +import android.content.Intent; +import android.content.IntentFilter; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.Preference; +import android.preference.Preference.OnPreferenceClickListener; +import android.preference.PreferenceActivity; //继承PreferenceActivity,主要功能为对系统配置进行自动保存以及实现用户同步的操作 +import android.preference.PreferenceCategory; +import android.text.TextUtils; +import android.text.format.DateFormat; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.widget.Button; //按钮 +import android.widget.TextView; +import android.widget.Toast; + +import net.micode.notes.R; +import net.micode.notes.data.Notes; //引入R包 +import net.micode.notes.data.Notes.NoteColumns; //小米便签栏 +import net.micode.notes.gtask.remote.GTaskSyncService; + //引用包库 + +public class NotesPreferenceActivity extends PreferenceActivity { //实现的是对背景颜色和字体大小的数据储存 + public static final String PREFERENCE_NAME = "notes_preferences"; //优先名 + + public static final String PREFERENCE_SYNC_ACCOUNT_NAME = "pref_key_account_name"; //同步账号 + + public static final String PREFERENCE_LAST_SYNC_TIME = "pref_last_sync_time"; //最后同步时间 + + public static final String PREFERENCE_SET_BG_COLOR_KEY = "pref_key_bg_random_appear"; //背景颜色的设置 + + private static final String PREFERENCE_SYNC_ACCOUNT_KEY = "pref_sync_account_key"; //账户同步密码 + + private static final String AUTHORITIES_FILTER_KEY = "authorities"; //本地密码 + + private PreferenceCategory mAccountCategory; //账户分组 + + private GTaskReceiver mReceiver; //同步任务接收器 + + private Account[] mOriAccounts; //账户 + + private boolean mHasAddedAccount; //账户的Hash函数 + + @Override //功能描述:创建活动,加载布局,初始化广播接收器 函数实现:除了调用父类的onCreate,还有一些自己的个性化配置 参数描述:@icicle Bundle携带了偏好的一些数据 @return 无 + protected void onCreate(Bundle icicle) { //新建Activity + super.onCreate(icicle); //执行父类创建函数 + + /* using the app icon for navigation */ + getActionBar().setDisplayHomeAsUpEnabled(true); //给左上角图标的左边加上一个返回的图标 + + addPreferencesFromResource(R.xml.preferences); //从xml获取preference来源 + mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY); //根据同步账户密码进行账户分组 + mReceiver = new GTaskReceiver(); //根据同步账户关键码来初始化分组 + IntentFilter filter = new IntentFilter(); //设置过滤项 + filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME); + registerReceiver(mReceiver, filter); //初始化同步组件 + + mOriAccounts = null; //初始化同步组件 + View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null); //从xml获取Listview + getListView().addHeaderView(header, null, true);//获取listvivew,ListView的作用:用于列出所有选择 + } //在listview组件上方添加其他组件 + + @Override //功能描述:重启活动,活动由不可见变为可见,自动如果用户添加了一个新账户,则同步账户 函数实现:除了调用父类的onCreate,还有一些自己的个性化配置 参数描述:@return 无 + protected void onResume() { //activity交互功能的实现,用于接受用户的输入 + super.onResume(); //activity交互功能的实现,用于接受用户的输入 + + // need to set sync account automatically if user has added a new + // account //执行父类的交互实现 + if (mHasAddedAccount) { //若用户新加了账户则自动设置同步账户 + Account[] accounts = getGoogleAccounts(); //获取google账户 + if (mOriAccounts != null && accounts.length > mOriAccounts.length) { //若账户不为空且账户增加 + for (Account accountNew : accounts) { //遍历账户 + boolean found = false; //更新账户 + for (Account accountOld : mOriAccounts) { // + if (TextUtils.equals(accountOld.name, accountNew.name)) { //循环判断当前账户列表中的账户是否与新建账户名相同 + found = true; //更新账户 + break; //若是没有找到旧的账户,那么同步账号中就只添加新账户 + } //若是没有找到旧的账户,那么同步账号中就只添加新账户 + } + if (!found) { // 如果没有找到,则设置同步账户 + setSyncAccount(accountNew.name); //保存该账户 + break; + } + } + } + } + + refreshUI(); //刷新界面 + } //刷新标签界面 + + @Override //功能描述:销毁活动 函数实现:除了调用父类的onCreate,还有一些自己的个性化配置 参数描述:无 + protected void onDestroy() { //销毁Activity + if (mReceiver != null) { //销毁接收器 + unregisterReceiver(mReceiver); //取消广播器的监听 + } //注销接收器 + super.onDestroy(); //执行销毁动作 + } //执行父类的销毁动作 + //重新设置账户信息 + private void loadAccountPreference() { //设置账户信息 + mAccountCategory.removeAll(); //移除所有分组 + + Preference accountPref = new Preference(this); //建立首选项 + final String defaultAccount = getSyncAccountName(this); //默认账户为当前账户 + accountPref.setTitle(getString(R.string.preferences_account_title)); //设置首选项的大标题和小标题 + accountPref.setSummary(getString(R.string.preferences_account_summary)); //与google task同步便签记录 + accountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() { //建立监听器 + public boolean onPreferenceClick(Preference preference) { //判断是否处于同步模式和默认的数据,指向不同的操作 + if (!GTaskSyncService.isSyncing()) { //不在同步状态下,如果没有默认的账户,显示选择账户的对话框,否则显示需要改变账户的对话框 + if (TextUtils.isEmpty(defaultAccount)) { //第一次设置账户 + // the first time to set account //第一次建立账户显示选择账户提示对话框 + showSelectAccountAlertDialog(); //选择需要同步的账号 + } else { //代码块:若是账户已经存在,则显示修改对话框并进行修改操作 + // if the account has already been set, we need to promp + // user about the risk + showChangeAccountConfirmAlertDialog(); //展示改变账户确认提醒对话框 + } + } else { //代码块:若在没有同步的情况下,则在toast中显示不能修改 + Toast.makeText(NotesPreferenceActivity.this, //若正在同步则显示不能修改账户 + R.string.preferences_toast_cannot_change_account, Toast.LENGTH_SHORT) + .show(); //正在同步,不能切换同步的账号 + } + return true; + } + }); //根据新建首选项添加账户分组 + + mAccountCategory.addPreference(accountPref); //根据新建首选项编辑新的账户分组 + } //根据新建首选项编辑新的账户分组 + //设置按键状态和最后的同步时间 + private void loadSyncButton() { //设置按键状态和最后的同步时间 + Button syncButton = (Button) findViewById(R.id.preference_sync_button); //获取同步按钮控件和最终同步时间的的窗口 + TextView lastSyncTimeView = (TextView) findViewById(R.id.prefenerece_sync_status_textview); //获取同步按键和同步时间显示 + //代码块:获取同步按钮控件和最终同步时间的的窗口 + // set button state + if (GTaskSyncService.isSyncing()) { //同步状态下按键显示“取消同步”,设置相关监听器 + syncButton.setText(getString(R.string.preferences_button_sync_cancel)); + syncButton.setOnClickListener(new View.OnClickListener() { //设置点击监听器 + public void onClick(View v) { //设置取消同步的响应方法 + GTaskSyncService.cancelSync(NotesPreferenceActivity.this); //响应点击的行为:取消同步 + } //设置按钮显示的文本为“取消同步”以及监听器 + }); + } else { //非同步状态下按键显示“立即同步”,设置相关监听器 + syncButton.setText(getString(R.string.preferences_button_sync_immediately)); //当前为不同步状态时显示“立即同步”按键,设置对应监听器 + syncButton.setOnClickListener(new View.OnClickListener() { //若是不同步则设置按钮显示的文本为“立即同步”以及对应监听器 + public void onClick(View v) { //点击行为 + GTaskSyncService.startSync(NotesPreferenceActivity.this); //开始同步 + } + }); //设置按键可用还是不可用 + }//非空 + syncButton.setEnabled(!TextUtils.isEmpty(getSyncAccountName(this))); //如果没有账户,则不可选“立即同步”的按键 + + // set last sync time //最终同步时间 + if (GTaskSyncService.isSyncing()) { //如果正在同步则读取正在同步的进度,否则显示最后同步的时间 + lastSyncTimeView.setText(GTaskSyncService.getProgressString()); + lastSyncTimeView.setVisibility(View.VISIBLE); //根据当前同步服务器设置时间显示框的文本以及可见性 + } else { //根据当前同步服务器设置时间显示框的文本以及可见性 + long lastSyncTime = getLastSyncTime(this); + if (lastSyncTime != 0) { //设置一些显示的内容 + lastSyncTimeView.setText(getString(R.string.preferences_last_sync_time, //非同步时,若最近同步时间不为0则显示最近同步时间 + DateFormat.format(getString(R.string.preferences_last_sync_time_format), + lastSyncTime))); //若是非同步情况 + lastSyncTimeView.setVisibility(View.VISIBLE); //将该视图设置为可见 + } else { //根据最后同步时间的信息来编辑时间显示框的文本内容和可见性 + lastSyncTimeView.setVisibility(View.GONE); //最近同步时间为空设置同步时间不可见 + } + } + } + //刷新界面 + private void refreshUI() { //刷新标签页面 + loadAccountPreference(); + loadSyncButton(); //加载“保存”按钮 + } + //设置选择账户的对话框 + private void showSelectAccountAlertDialog() { //显示账户选择的对话框并进行账户的设置 + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); //创建一个新的对话框 + + View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null); + TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title); //文本视图设置 + titleTextView.setText(getString(R.string.preferences_dialog_select_account_title)); + TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle); + subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips)); + + dialogBuilder.setCustomTitle(titleView); //设置标题以及子标题的内容 + dialogBuilder.setPositiveButton(null, null); //不设置“确定”的按钮 + + Account[] accounts = getGoogleAccounts(); //获取当前谷歌账户列表 + String defAccount = getSyncAccountName(this);//得到同步账户 + + mOriAccounts = accounts; //获取同步账户信息 + mHasAddedAccount = false; + + if (accounts.length > 0) { //如果有谷歌账户,则显示所有账户的名称,并设置为选项 + CharSequence[] items = new CharSequence[accounts.length]; + final CharSequence[] itemMapping = items; + int checkedItem = -1; + int index = 0; + for (Account account : accounts) { //通过循环检查账户列表 + if (TextUtils.equals(account.name, defAccount)) { + checkedItem = index; + } //在账户列表中查询到所需账户 + items[index++] = account.name; + } + dialogBuilder.setSingleChoiceItems(items, checkedItem, //在对话框建立一个单选的复选框 + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { //添加点击监听器 + setSyncAccount(itemMapping[which].toString()); //点击则开始设置同步账户 + dialog.dismiss(); //取消对话框 + refreshUI(); //刷新界面 + } //设置点击后执行的事件,包括检录新同步账户和刷新标签界面 + }); //若时间为空直接设置为不可见状态 + } + + View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null); //添加新的账户 + dialogBuilder.setView(addAccountView); //给新加账户对话框设置自定义样式 + + final AlertDialog dialog = dialogBuilder.show(); //显示对话框 + addAccountView.setOnClickListener(new View.OnClickListener() { //设置监听器 + public void onClick(View v) { //响应点击添加账户的请求 + mHasAddedAccount = true; //将新加账户的hash置为true + Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS"); //建立网络组件 + intent.putExtra(AUTHORITIES_FILTER_KEY, new String[] { + "gmail-ls" + }); + startActivityForResult(intent, -1); //当点击添加账户时,创建新活动,因为请求码是-1,所以此活动结束后不会反馈给源活动 + dialog.dismiss(); + } + }); + } + + private void showChangeAccountConfirmAlertDialog() { //刷新标签界面 + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); //创建一个新的对话框 + + View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null); + TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title); + titleTextView.setText(getString(R.string.preferences_dialog_change_account_title, + getSyncAccountName(this))); + TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle); + subtitleTextView.setText(getString(R.string.preferences_dialog_change_account_warn_msg)); //根据同步修改的账户信息设置标题以及子标题的内容 + dialogBuilder.setCustomTitle(titleView); + + CharSequence[] menuItemArray = new CharSequence[] { //设置对话框的自定义标题 + getString(R.string.preferences_menu_change_account), + getString(R.string.preferences_menu_remove_account), + getString(R.string.preferences_menu_cancel) + }; + dialogBuilder.setItems(menuItemArray, new DialogInterface.OnClickListener() { //定义一些标记字符串 + public void onClick(DialogInterface dialog, int which) { // 响应对话框的点击 + if (which == 0) { //进入账户选择对话框 + showSelectAccountAlertDialog(); //显示账户选择提示对话框 + } else if (which == 1) { //删除同步账户 + removeSyncAccount(); //删除账户并且跟新便签界面 + refreshUI(); + } + } + }); //新建提示对话框 + dialogBuilder.show(); //显示对话框 + } + + private Account[] getGoogleAccounts() { //获取谷歌账户,可通过账户管理器直接获取 + AccountManager accountManager = AccountManager.get(this); + return accountManager.getAccountsByType("com.google"); + } + //设置同步账户 + private void setSyncAccount(String account) { //设置同步账户 + if (!getSyncAccountName(this).equals(account)) { //如果该账号不在同步账号列表中 + SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = settings.edit(); //编辑共享首选项 + if (account != null) { // 编辑共享的首选项 + editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, account); + } else { + editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, ""); + } //将该账号加入到首选项中 + editor.commit(); //提交修改的数据 + + // clean up last sync time + setLastSyncTime(this, 0); //清除最后同步的时间 + + // clean up local gtask related info + new Thread(new Runnable() { + public void run() { // 新线程的创建 + ContentValues values = new ContentValues(); + values.put(NoteColumns.GTASK_ID, ""); + values.put(NoteColumns.SYNC_ID, 0); + getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null); + } + }).start(); //重置当地同步任务的信息 + //设置一个toast提示信息,提示用户成功设置同步 + Toast.makeText(NotesPreferenceActivity.this, //设置一个toast提示信息,提示用户成功设置同步 + getString(R.string.preferences_toast_success_set_accout, account), + Toast.LENGTH_SHORT).show(); //将toast的文本信息置为“设置账户成功”并显示出来 + } + } + //删除同步账户 + private void removeSyncAccount() { //删除同步账户 + SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); //SharedPreferences是以键值对的形式存储数据的,其使用非常简单,能够轻松的存放数据和读取数据 + SharedPreferences.Editor editor = settings.edit(); //设置共享首选项 + if (settings.contains(PREFERENCE_SYNC_ACCOUNT_NAME)) { //假如当前首选项中有账户就删除 + editor.remove(PREFERENCE_SYNC_ACCOUNT_NAME); + } + if (settings.contains(PREFERENCE_LAST_SYNC_TIME)) {//删除当前首选项中有账户时间 + editor.remove(PREFERENCE_LAST_SYNC_TIME); //如果包含其中就将时间也清除 + } + editor.commit(); //提交更新后的数据 + + // clean up local gtask related info + new Thread(new Runnable() { //新线程的创建 + public void run() { //清除本地的gtask关联的信息,将一些参数设置为0或NULL + ContentValues values = new ContentValues(); + values.put(NoteColumns.GTASK_ID, ""); + values.put(NoteColumns.SYNC_ID, 0); + getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null); + } + }).start(); + } + //得到同步账户的名称 + public static String getSyncAccountName(Context context) { //获取同步账户名称,通过共享的首选项里的信息直接获取 + SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, + Context.MODE_PRIVATE); //获取同步账户名称 + return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, ""); + } + //设置最终同步的时间 + public static void setLastSyncTime(Context context, long time) { //设置最终同步的时间 + SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, + Context.MODE_PRIVATE); + SharedPreferences.Editor editor = settings.edit(); //从共享首选项中找到相关账户并获取其编辑器 + editor.putLong(PREFERENCE_LAST_SYNC_TIME, time); + editor.commit(); //编辑最终同步时间并提交更新 + } + // 功能描述:得到最后同步的时间 函数实现:调用SharedPreferences里的方法 参数描述:@content 是有关的内容项 + public static long getLastSyncTime(Context context) { //获取最终同步时间,通过共享的首选项里的信息直接获取 + SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, //通过共享,获取时间 + Context.MODE_PRIVATE); + return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0); + } + + private class GTaskReceiver extends BroadcastReceiver { //接受同步信息 + + @Override + public void onReceive(Context context, Intent intent) { //响应收到广播的情况 + refreshUI(); + if (intent.getBooleanExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING, false)) { //获取随广播而来的Intent中的同步服务的数据 + TextView syncStatus = (TextView) findViewById(R.id.prefenerece_sync_status_textview); //通过获取的数据在设置系统的状态 + syncStatus.setText(intent + .getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG)); + } //通过获取的数据在设置系统的状态 + + } + } + //处理菜单的选项 + public boolean onOptionsItemSelected(MenuItem item) { //处理菜单的选项 + switch (item.getItemId()) {//根据选项的id选择,这里只有一个主页 + case android.R.id.home: + Intent intent = new Intent(this, NotesListActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + startActivity(intent); //创建活动 + return true; + default: //在主页情况下在创建连接组件intent,发出清空的信号并开始一个相应的activity + return false; + } + } +} From 26e0dbb1802701146fc72dbdb9710d521072307f Mon Sep 17 00:00:00 2001 From: iris <2191666430@qq.com> Date: Tue, 11 Apr 2023 21:12:15 +0800 Subject: [PATCH 4/7] 1 --- doc/吴亚婷精读部分/data/MetaData.java | 83 +++ doc/吴亚婷精读部分/data/Node.java | 102 +++ doc/吴亚婷精读部分/data/SqlData.java | 189 ++++++ doc/吴亚婷精读部分/data/SqlNote.java | 505 +++++++++++++++ doc/吴亚婷精读部分/data/Task.java | 351 +++++++++++ doc/吴亚婷精读部分/data/TaskList.java | 346 +++++++++++ .../exception/ActionFailureException.java | 33 + .../exception/NetworkFailureException.java | 33 + .../remote/GTaskClient.java | 585 ++++++++++++++++++ 9 files changed, 2227 insertions(+) create mode 100644 doc/吴亚婷精读部分/data/MetaData.java create mode 100644 doc/吴亚婷精读部分/data/Node.java create mode 100644 doc/吴亚婷精读部分/data/SqlData.java create mode 100644 doc/吴亚婷精读部分/data/SqlNote.java create mode 100644 doc/吴亚婷精读部分/data/Task.java create mode 100644 doc/吴亚婷精读部分/data/TaskList.java create mode 100644 doc/吴亚婷精读部分/exception/ActionFailureException.java create mode 100644 doc/吴亚婷精读部分/exception/NetworkFailureException.java create mode 100644 doc/吴亚婷精读部分/remote/GTaskClient.java diff --git a/doc/吴亚婷精读部分/data/MetaData.java b/doc/吴亚婷精读部分/data/MetaData.java new file mode 100644 index 0000000..ab5f5fd --- /dev/null +++ b/doc/吴亚婷精读部分/data/MetaData.java @@ -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.gtask.data; +//本类继承于Task,主要用于记录数据的变化 + +import android.database.Cursor;//基于数据库服务的类 +import android.util.Log;//日志工具类,提供了5个方法来供我们打印日志 + +import net.micode.notes.tool.GTaskStringUtils;//导入tool包中GTaskStringUtils类的方法 + +import org.json.JSONException;//Json使用失败异常处理 +import org.json.JSONObject;//存储json对象的属性名和属性值 + + +public class MetaData extends Task { + private final static String TAG = MetaData.class.getSimpleName(); +//调用getSimpleName()函数来得到类的简写名称存入字符串TAG中 + private String mRelatedGid = null;//初始化mRelatedGid的值 + + public void setMeta(String gid, JSONObject metaInfo) { + try { + metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid);//将这对键值放入metaInfo这个jsonobject对象中 + } catch (JSONException e) { + Log.e(TAG, "failed to put related gid"); + }//捕捉异常,输出错误信息 + setNotes(metaInfo.toString());//设置便签,将Json类的metaInfo转为string + setName(GTaskStringUtils.META_NOTE_NAME);//设置gtask的名字 + }//设置数据,生成元数据库 + + public String getRelatedGid() { + return mRelatedGid; + }//获取相关联的Gid + + @Override + public boolean isWorthSaving() { + return getNotes() != null; + }//判断当前数据是否有效,若数据非空则返回真值 + + @Override + public void setContentByRemoteJSON(JSONObject js) { + super.setContentByRemoteJSON(js);//调用父类Task中的setContentByRemoteJSON()函数 + if (getNotes() != null) { //如果数据非空,获取jsono metainfo和相关gid + try { + JSONObject metaInfo = new JSONObject(getNotes().trim()); + mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID); + } catch (JSONException e) { + Log.w(TAG, "failed to get related gid"); + mRelatedGid = null; + } + } + }//调用父类Task中的setContentByRemoteJSON()函数,使用远程json数据对象设置元数据内容 + + @Override + public void setContentByLocalJSON(JSONObject js) { + // this function should not be called + throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called"); + }//使用本地json数据对象设置元数据内容,一般不会用到,若用到,则抛出异常 + + @Override + public JSONObject getLocalJSONFromContent() { + throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called"); + }//从元数据内容中获取本地json对象 + + @Override + public int getSyncAction(Cursor c) { + throw new IllegalAccessError("MetaData:getSyncAction should not be called"); + }//获取同步动作状态,一般不会用到,若用到,则抛出异常 + +}//创建一个继承Task的类MataData diff --git a/doc/吴亚婷精读部分/data/Node.java b/doc/吴亚婷精读部分/data/Node.java new file mode 100644 index 0000000..daacc88 --- /dev/null +++ b/doc/吴亚婷精读部分/data/Node.java @@ -0,0 +1,102 @@ +/* + * 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.gtask.data;//包名,说明依赖关系 + +import android.database.Cursor;//引用cursor,此公用接口提供对数据库查询返回的结果集的随机读写访问 + +import org.json.JSONObject;//引用JSONObject,可修改名称/值 + +public abstract class Node { + //同步操作的基础数据类型,定义了相关指示同步操作的常量 + public static final int SYNC_ACTION_NONE = 0;//本地和云端均无更新 + + public static final int SYNC_ACTION_ADD_REMOTE = 1;//在云端增加内容 + + public static final int SYNC_ACTION_ADD_LOCAL = 2;//在本地增加内容 + + public static final int SYNC_ACTION_DEL_REMOTE = 3;//在云端删除内容 + + public static final int SYNC_ACTION_DEL_LOCAL = 4;//在本地删除内容 + + public static final int SYNC_ACTION_UPDATE_REMOTE = 5;//将本地内容更新到云端 + + public static final int SYNC_ACTION_UPDATE_LOCAL = 6;//将云端内容更新到本地 + + public static final int SYNC_ACTION_UPDATE_CONFLICT = 7;//同步出现冲突 + + public static final int SYNC_ACTION_ERROR = 8;//同步出现错误 + + private String mGid;//记录gid + + private String mName;//记录name + + private long mLastModified;//记录最后行为时间 + + private boolean mDeleted;//记录是否被删除 + + public Node() { + mGid = null; + mName = ""; + mLastModified = 0; + mDeleted = false; + }//构造函数 + + public abstract JSONObject getCreateAction(int actionId);//获取创建信息 + + public abstract JSONObject getUpdateAction(int actionId);//获取更新行为 + + public abstract void setContentByRemoteJSON(JSONObject js);//创建相应对象进行远端操作 + + public abstract void setContentByLocalJSON(JSONObject js);//创建相应对象进行本地操作 + + public abstract JSONObject getLocalJSONFromContent();//从内容获取本地JSON对象 + + public abstract int getSyncAction(Cursor c);//获取同步行为代号 + //对构造函数中的对象进行赋值或者获取对象的具体内容 + public void setGid(String gid) { + this.mGid = gid; + } + + public void setName(String name) { + this.mName = name; + } + + public void setLastModified(long lastModified) { + this.mLastModified = lastModified; + } + + public void setDeleted(boolean deleted) { + this.mDeleted = deleted; + } + + public String getGid() { + return this.mGid; + } + + public String getName() { + return this.mName; + } + + public long getLastModified() { + return this.mLastModified; + } + + public boolean getDeleted() { + return this.mDeleted; + } + +}//抽象类,声明若干基本数据成员,提供给别的类继承使用 diff --git a/doc/吴亚婷精读部分/data/SqlData.java b/doc/吴亚婷精读部分/data/SqlData.java new file mode 100644 index 0000000..3dd9e0c --- /dev/null +++ b/doc/吴亚婷精读部分/data/SqlData.java @@ -0,0 +1,189 @@ +/* + * 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.gtask.data;//包名,说明归属关系 + +import android.content.ContentResolver;//引用android的一些操作,下同 +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.util.Log; + +import net.micode.notes.data.Notes;//引用本地包中的类,下同 +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.DataConstants; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.data.NotesDatabaseHelper.TABLE; +import net.micode.notes.gtask.exception.ActionFailureException; + +import org.json.JSONException;//引用org.json的一些操作 +import org.json.JSONObject; + + +public class SqlData { + private static final String TAG = SqlData.class.getSimpleName();//得到类的简写名称存入TAG中 + + private static final int INVALID_ID = -99999; + + public static final String[] PROJECTION_DATA = new String[] { + DataColumns.ID, DataColumns.MIME_TYPE, DataColumns.CONTENT, DataColumns.DATA1, + DataColumns.DATA3 + };//新建字符串数组。集合interface DataColumns中所有SF常量 + //以下五个变量作为SQL表中五列的编号 + public static final int DATA_ID_COLUMN = 0; + + public static final int DATA_MIME_TYPE_COLUMN = 1; + + public static final int DATA_CONTENT_COLUMN = 2; + + public static final int DATA_CONTENT_DATA_1_COLUMN = 3; + + public static final int DATA_CONTENT_DATA_3_COLUMN = 4; + //定义以下8个私有全局变量,与后来的SqlNote对应 + private ContentResolver mContentResolver; + + private boolean mIsCreate; + + private long mDataId; + + private String mDataMimeType; + + private String mDataContent; + + private long mDataContentData1; + + private String mDataContentData3; + + private ContentValues mDiffDataValues; + + public SqlData(Context context) { + mContentResolver = context.getContentResolver(); + mIsCreate = true; + mDataId = INVALID_ID; + mDataMimeType = DataConstants.NOTE; + mDataContent = ""; + mDataContentData1 = 0; + mDataContentData3 = ""; + mDiffDataValues = new ContentValues(); + }//构造函数,参数类型为Context,更倾向于默认构造函数 + + public SqlData(Context context, Cursor c) { + mContentResolver = context.getContentResolver(); + mIsCreate = false; + loadFromCursor(c); + mDiffDataValues = new ContentValues(); + }//构造函数,参数类型为Context和Cursor,由游标c把带来的信息替换掉初始值 + + private void loadFromCursor(Cursor c) { + mDataId = c.getLong(DATA_ID_COLUMN); + mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN); + mDataContent = c.getString(DATA_CONTENT_COLUMN); + mDataContentData1 = c.getLong(DATA_CONTENT_DATA_1_COLUMN); + mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN); + }//从光标c处将五列数据加载到该类对象 + + public void setContent(JSONObject js) throws JSONException { + long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID; + if (mIsCreate || mDataId != dataId) { + mDiffDataValues.put(DataColumns.ID, dataId); + } + mDataId = dataId;//与共享数据库同步后,共享数据ID就等于数据ID + + String dataMimeType = js.has(DataColumns.MIME_TYPE) ? js.getString(DataColumns.MIME_TYPE) + : DataConstants.NOTE; + if (mIsCreate || !mDataMimeType.equals(dataMimeType)) { + mDiffDataValues.put(DataColumns.MIME_TYPE, dataMimeType); + } + mDataMimeType = dataMimeType;//共享数据同步后,共享数据mime类型等于该数据mime类型 + + String dataContent = js.has(DataColumns.CONTENT) ? js.getString(DataColumns.CONTENT) : ""; + if (mIsCreate || !mDataContent.equals(dataContent)) { + mDiffDataValues.put(DataColumns.CONTENT, dataContent); + } + mDataContent = dataContent;//共享数据同步后,共享数据内容等于该数据内容 + + long dataContentData1 = js.has(DataColumns.DATA1) ? js.getLong(DataColumns.DATA1) : 0; + if (mIsCreate || mDataContentData1 != dataContentData1) { + mDiffDataValues.put(DataColumns.DATA1, dataContentData1); + } + mDataContentData1 = dataContentData1;//共享数据同步后,共享1类型数据等于该1类型数据 + + String dataContentData3 = js.has(DataColumns.DATA3) ? js.getString(DataColumns.DATA3) : ""; + if (mIsCreate || !mDataContentData3.equals(dataContentData3)) { + mDiffDataValues.put(DataColumns.DATA3, dataContentData3); + } + mDataContentData3 = dataContentData3;//共享数据同步后,共享3类型数据等于该3类型数据 + }//设置共享数据,并提供抛出异常与处理机制 + + public JSONObject getContent() throws JSONException { + if (mIsCreate) { + Log.e(TAG, "it seems that we haven't created this in database yet"); + return null; + } + JSONObject js = new JSONObject(); + js.put(DataColumns.ID, mDataId); + js.put(DataColumns.MIME_TYPE, mDataMimeType); + js.put(DataColumns.CONTENT, mDataContent); + js.put(DataColumns.DATA1, mDataContentData1); + js.put(DataColumns.DATA3, mDataContentData3); + return js; + }//获取用于共享的数据内容,并提供异常抛出与处理机制 + + public void commit(long noteId, boolean validateVersion, long version) { +//分两种构造方式进行不同的操作,之后进行异常处理并反馈错误信息, 下同 + if (mIsCreate) { + if (mDataId == INVALID_ID && mDiffDataValues.containsKey(DataColumns.ID)) { + mDiffDataValues.remove(DataColumns.ID); + } + + mDiffDataValues.put(DataColumns.NOTE_ID, noteId); + Uri uri = mContentResolver.insert(Notes.CONTENT_DATA_URI, mDiffDataValues); + try { + mDataId = Long.valueOf(uri.getPathSegments().get(1)); + } catch (NumberFormatException e) { + Log.e(TAG, "Get note id error :" + e.toString()); + throw new ActionFailureException("create note failed"); + } + } else { + if (mDiffDataValues.size() > 0) { + int result = 0; + if (!validateVersion) { + result = mContentResolver.update(ContentUris.withAppendedId( + Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues, null, null); + } else { + result = mContentResolver.update(ContentUris.withAppendedId( + Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues, + " ? in (SELECT " + NoteColumns.ID + " FROM " + TABLE.NOTE + + " WHERE " + NoteColumns.VERSION + "=?)", new String[] { + String.valueOf(noteId), String.valueOf(version) + }); + } + if (result == 0) { + Log.w(TAG, "there is no update. maybe user updates note when syncing"); + } + } + } + + mDiffDataValues.clear(); + mIsCreate = false; + }//将当前操作所做的修改保存到数据库 + + public long getId() { + return mDataId; + }//获取当前ID +}//支持小米便签最底层的数据库相关操作 diff --git a/doc/吴亚婷精读部分/data/SqlNote.java b/doc/吴亚婷精读部分/data/SqlNote.java new file mode 100644 index 0000000..edaa3e1 --- /dev/null +++ b/doc/吴亚婷精读部分/data/SqlNote.java @@ -0,0 +1,505 @@ +/* + * 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.gtask.data; + +import android.appwidget.AppWidgetManager;//引用android的一些操作,下同 +import android.content.ContentResolver; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.util.Log; + +import net.micode.notes.data.Notes;//引用本地包中的类,下同 +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.gtask.exception.ActionFailureException; +import net.micode.notes.tool.GTaskStringUtils; +import net.micode.notes.tool.ResourceParser; + +import org.json.JSONArray;//引用org.json的一些操作 +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; + + +public class SqlNote { + private static final String TAG = SqlNote.class.getSimpleName();//得到类的简写名称存入TAG中 + + private static final int INVALID_ID = -99999; + + public static final String[] PROJECTION_NOTE = new String[] { + NoteColumns.ID, NoteColumns.ALERTED_DATE, NoteColumns.BG_COLOR_ID, + NoteColumns.CREATED_DATE, NoteColumns.HAS_ATTACHMENT, NoteColumns.MODIFIED_DATE, + NoteColumns.NOTES_COUNT, NoteColumns.PARENT_ID, NoteColumns.SNIPPET, NoteColumns.TYPE, + NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE, NoteColumns.SYNC_ID, + NoteColumns.LOCAL_MODIFIED, NoteColumns.ORIGIN_PARENT_ID, NoteColumns.GTASK_ID, + NoteColumns.VERSION + };//集合interface NoteColumns中所有17个SF常量 + //以下设置17个列的编号,对应不同属性 + public static final int ID_COLUMN = 0;//ID + + public static final int ALERTED_DATE_COLUMN = 1;//提醒时间 + + public static final int BG_COLOR_ID_COLUMN = 2;//背景颜色 + + public static final int CREATED_DATE_COLUMN = 3;//创建时间 + + public static final int HAS_ATTACHMENT_COLUMN = 4;//有无附件 + + public static final int MODIFIED_DATE_COLUMN = 5;//修改时间 + + public static final int NOTES_COUNT_COLUMN = 6;//便签数 + + public static final int PARENT_ID_COLUMN = 7;//父节点ID + + public static final int SNIPPET_COLUMN = 8;//文本片段 + + public static final int TYPE_COLUMN = 9;//文件类型 + + public static final int WIDGET_ID_COLUMN = 10;//窗口小部件ID + + public static final int WIDGET_TYPE_COLUMN = 11;//小部件种类 + + public static final int SYNC_ID_COLUMN = 12;//同步ID + + public static final int LOCAL_MODIFIED_COLUMN = 13;//本地修改 + + public static final int ORIGIN_PARENT_ID_COLUMN = 14;//在进入临时文件夹之前,原始的父ID + + public static final int GTASK_ID_COLUMN = 15;//用户ID + + public static final int VERSION_COLUMN = 16;//版本号 + //定义17个内部变量,帮助构造SqlNote + private Context mContext; + + private ContentResolver mContentResolver; + + private boolean mIsCreate; + + private long mId; + + private long mAlertDate; + + private int mBgColorId; + + private long mCreatedDate; + + private int mHasAttachment; + + private long mModifiedDate; + + private long mParentId; + + private String mSnippet; + + private int mType; + + private int mWidgetId; + + private int mWidgetType; + + private long mOriginParent; + + private long mVersion; + + private ContentValues mDiffNoteValues; + + private ArrayList mDataList; + + public SqlNote(Context context) { + mContext = context; + mContentResolver = context.getContentResolver(); + mIsCreate = true; + mId = INVALID_ID; + mAlertDate = 0; + mBgColorId = ResourceParser.getDefaultBgId(context); + mCreatedDate = System.currentTimeMillis(); + mHasAttachment = 0; + mModifiedDate = System.currentTimeMillis(); + mParentId = 0; + mSnippet = ""; + mType = Notes.TYPE_NOTE; + mWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; + mWidgetType = Notes.TYPE_WIDGET_INVALIDE; + mOriginParent = 0; + mVersion = 0; + mDiffNoteValues = new ContentValues(); + mDataList = new ArrayList(); + }//构造函数,参数类型为Context,倾向于默认构造函数 + + public SqlNote(Context context, Cursor c) { + mContext = context; + mContentResolver = context.getContentResolver(); + mIsCreate = false; + loadFromCursor(c); + mDataList = new ArrayList(); + if (mType == Notes.TYPE_NOTE) + loadDataContent(); + mDiffNoteValues = new ContentValues(); + }//构造函数,参数类型有Context和Cursor,对游标指向的对象进行初始化 + + public SqlNote(Context context, long id) { + mContext = context; + mContentResolver = context.getContentResolver(); + mIsCreate = false; + loadFromCursor(id); + mDataList = new ArrayList(); + if (mType == Notes.TYPE_NOTE) + loadDataContent(); + mDiffNoteValues = new ContentValues(); + + }//构造函数,参数有context和id,对id指向的对象进行初始化 + + private void loadFromCursor(long id) { + Cursor c = null; + try { + c = mContentResolver.query(Notes.CONTENT_NOTE_URI, PROJECTION_NOTE, "(_id=?)", + new String[] { + String.valueOf(id) + }, null); + if (c != null) { + c.moveToNext(); + loadFromCursor(c); + } else { + Log.w(TAG, "loadFromCursor: cursor = null"); + } + } finally { + if (c != null) + c.close(); + } + }//通过id获取ContentResolver中相应内容,并赋给cursor,并提供抛出异常和处理机制 + + private void loadFromCursor(Cursor c) { + mId = c.getLong(ID_COLUMN);//直接从游标指向的那条记录中加载相应数据 + mAlertDate = c.getLong(ALERTED_DATE_COLUMN); + mBgColorId = c.getInt(BG_COLOR_ID_COLUMN); + mCreatedDate = c.getLong(CREATED_DATE_COLUMN); + mHasAttachment = c.getInt(HAS_ATTACHMENT_COLUMN); + mModifiedDate = c.getLong(MODIFIED_DATE_COLUMN); + mParentId = c.getLong(PARENT_ID_COLUMN); + mSnippet = c.getString(SNIPPET_COLUMN); + mType = c.getInt(TYPE_COLUMN); + mWidgetId = c.getInt(WIDGET_ID_COLUMN); + mWidgetType = c.getInt(WIDGET_TYPE_COLUMN); + mVersion = c.getLong(VERSION_COLUMN); + }//从游标处获取并赋值各变量 + + private void loadDataContent() { + Cursor c = null; + mDataList.clear();//清空数据列表 + try { + c = mContentResolver.query(Notes.CONTENT_DATA_URI, SqlData.PROJECTION_DATA,//获得ID对应的数据内容 + "(note_id=?)", new String[] { + String.valueOf(mId) + }, null); + if (c != null) { + if (c.getCount() == 0) { + Log.w(TAG, "it seems that the note has not data"); + return; + }//如果光标处无内容,提示NOTE无数据报错 + while (c.moveToNext()) { + SqlData data = new SqlData(mContext, c); + mDataList.add(data); + }//循环获取数据加载进datalist中 + } else { + Log.w(TAG, "loadDataContent: cursor = null"); + }//如果cursor为空,则提示报错 + } finally { + if (c != null) + c.close(); + }//最后若游标不为空,关闭游标 + }//获取共享数据并加载到数据库当前游标处 + + public boolean setContent(JSONObject js) { + try { + JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);//创建一个JSONObject对象note + if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) { + Log.w(TAG, "cannot set system folder");//不能对系统文件夹进行设置操作 + } else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) { + // for folder we can only update the snnipet and type + String snippet = note.has(NoteColumns.SNIPPET) ? note//如果共享数据存在摘要,将其赋给snippet变量,否则变量为空 + .getString(NoteColumns.SNIPPET) : ""; + if (mIsCreate || !mSnippet.equals(snippet)) { + mDiffNoteValues.put(NoteColumns.SNIPPET, snippet); + }//如果没有创建或该摘要没有匹配原摘要则将其加入解析器 + mSnippet = snippet;//将该摘要覆盖原摘要 + //以下操作都与上面对snippet的操作一样,一起根据共享的数据设置SqlNote内容的上述17项 + int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE) + : Notes.TYPE_NOTE; + if (mIsCreate || mType != type) { + mDiffNoteValues.put(NoteColumns.TYPE, type); + } + mType = type; + } else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_NOTE) { + JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA); + long id = note.has(NoteColumns.ID) ? note.getLong(NoteColumns.ID) : INVALID_ID; + if (mIsCreate || mId != id) { + mDiffNoteValues.put(NoteColumns.ID, id); + } + mId = id; + + long alertDate = note.has(NoteColumns.ALERTED_DATE) ? note + .getLong(NoteColumns.ALERTED_DATE) : 0; + if (mIsCreate || mAlertDate != alertDate) { + mDiffNoteValues.put(NoteColumns.ALERTED_DATE, alertDate); + } + mAlertDate = alertDate; + + int bgColorId = note.has(NoteColumns.BG_COLOR_ID) ? note + .getInt(NoteColumns.BG_COLOR_ID) : ResourceParser.getDefaultBgId(mContext); + if (mIsCreate || mBgColorId != bgColorId) { + mDiffNoteValues.put(NoteColumns.BG_COLOR_ID, bgColorId); + } + mBgColorId = bgColorId; + + long createDate = note.has(NoteColumns.CREATED_DATE) ? note + .getLong(NoteColumns.CREATED_DATE) : System.currentTimeMillis(); + if (mIsCreate || mCreatedDate != createDate) { + mDiffNoteValues.put(NoteColumns.CREATED_DATE, createDate); + } + mCreatedDate = createDate; + + int hasAttachment = note.has(NoteColumns.HAS_ATTACHMENT) ? note + .getInt(NoteColumns.HAS_ATTACHMENT) : 0; + if (mIsCreate || mHasAttachment != hasAttachment) { + mDiffNoteValues.put(NoteColumns.HAS_ATTACHMENT, hasAttachment); + } + mHasAttachment = hasAttachment; + + long modifiedDate = note.has(NoteColumns.MODIFIED_DATE) ? note + .getLong(NoteColumns.MODIFIED_DATE) : System.currentTimeMillis(); + if (mIsCreate || mModifiedDate != modifiedDate) { + mDiffNoteValues.put(NoteColumns.MODIFIED_DATE, modifiedDate); + } + mModifiedDate = modifiedDate; + + long parentId = note.has(NoteColumns.PARENT_ID) ? note + .getLong(NoteColumns.PARENT_ID) : 0; + if (mIsCreate || mParentId != parentId) { + mDiffNoteValues.put(NoteColumns.PARENT_ID, parentId); + } + mParentId = parentId; + + String snippet = note.has(NoteColumns.SNIPPET) ? note + .getString(NoteColumns.SNIPPET) : ""; + if (mIsCreate || !mSnippet.equals(snippet)) { + mDiffNoteValues.put(NoteColumns.SNIPPET, snippet); + } + mSnippet = snippet; + + int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE) + : Notes.TYPE_NOTE; + if (mIsCreate || mType != type) { + mDiffNoteValues.put(NoteColumns.TYPE, type); + } + mType = type; + + int widgetId = note.has(NoteColumns.WIDGET_ID) ? note.getInt(NoteColumns.WIDGET_ID) + : AppWidgetManager.INVALID_APPWIDGET_ID; + if (mIsCreate || mWidgetId != widgetId) { + mDiffNoteValues.put(NoteColumns.WIDGET_ID, widgetId); + } + mWidgetId = widgetId; + + int widgetType = note.has(NoteColumns.WIDGET_TYPE) ? note + .getInt(NoteColumns.WIDGET_TYPE) : Notes.TYPE_WIDGET_INVALIDE; + if (mIsCreate || mWidgetType != widgetType) { + mDiffNoteValues.put(NoteColumns.WIDGET_TYPE, widgetType); + } + mWidgetType = widgetType; + + long originParent = note.has(NoteColumns.ORIGIN_PARENT_ID) ? note + .getLong(NoteColumns.ORIGIN_PARENT_ID) : 0; + if (mIsCreate || mOriginParent != originParent) { + mDiffNoteValues.put(NoteColumns.ORIGIN_PARENT_ID, originParent); + } + mOriginParent = originParent; + + for (int i = 0; i < dataArray.length(); i++) { + JSONObject data = dataArray.getJSONObject(i);//依次获取数据表中的数据ID + SqlData sqlData = null;//若SQL日期无就新建一个 + if (data.has(DataColumns.ID)) { + long dataId = data.getLong(DataColumns.ID); + for (SqlData temp : mDataList) { + if (dataId == temp.getId()) { + sqlData = temp; + } + } + }//该数据ID对应的数据如果存在,将对应的数据存在数据库中 + + if (sqlData == null) { + sqlData = new SqlData(mContext); + mDataList.add(sqlData); + }//如果数据库数据没有进行更新 + + sqlData.setContent(data); + }//遍历dataArray,查找ID为datald的数据 + } + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + return false; + } + return true; + }//设置通过content机制用于共享的数据信息 + + public JSONObject getContent() { + try { + JSONObject js = new JSONObject(); + + if (mIsCreate) { + Log.e(TAG, "it seems that we haven't created this in database yet"); + return null; + } + + JSONObject note = new JSONObject();//创建变量note用于传输共享数据 + if (mType == Notes.TYPE_NOTE) { //如果对象是note类型,设置以下12个内部变量 + note.put(NoteColumns.ID, mId); + note.put(NoteColumns.ALERTED_DATE, mAlertDate); + note.put(NoteColumns.BG_COLOR_ID, mBgColorId); + note.put(NoteColumns.CREATED_DATE, mCreatedDate); + note.put(NoteColumns.HAS_ATTACHMENT, mHasAttachment); + note.put(NoteColumns.MODIFIED_DATE, mModifiedDate); + note.put(NoteColumns.PARENT_ID, mParentId); + note.put(NoteColumns.SNIPPET, mSnippet); + note.put(NoteColumns.TYPE, mType); + note.put(NoteColumns.WIDGET_ID, mWidgetId); + note.put(NoteColumns.WIDGET_TYPE, mWidgetType); + note.put(NoteColumns.ORIGIN_PARENT_ID, mOriginParent); + js.put(GTaskStringUtils.META_HEAD_NOTE, note);//将note存入元数据中 + + JSONArray dataArray = new JSONArray();//将元数据存入数组中 + for (SqlData sqlData : mDataList) { + JSONObject data = sqlData.getContent(); + if (data != null) { + dataArray.put(data); + } + }//利用循环将note中所有数据存进dataarray中 + js.put(GTaskStringUtils.META_HEAD_DATA, dataArray); + } else if (mType == Notes.TYPE_FOLDER || mType == Notes.TYPE_SYSTEM) { + note.put(NoteColumns.ID, mId); + note.put(NoteColumns.TYPE, mType); + note.put(NoteColumns.SNIPPET, mSnippet); + js.put(GTaskStringUtils.META_HEAD_NOTE, note); + }//对象类型为系统文件或目录文件时,将ID,类型以及摘要存入JSONObject + + return js; + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + } + return null; + }//获取content机制提供的数据并加载到note中 + + public void setParentId(long id) { + mParentId = id; + mDiffNoteValues.put(NoteColumns.PARENT_ID, id); + }//给当前id设置父id + + public void setGtaskId(String gid) { + mDiffNoteValues.put(NoteColumns.GTASK_ID, gid); + }//给当前id设置Gtaskid + + public void setSyncId(long syncId) { + mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId); + }//给当前id设置同步id + + public void resetLocalModified() { + mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0); + }//撤销本地修改 + + public long getId() { + return mId; + }获得当前id + + public long getParentId() { + return mParentId; + }获得当前id的父id + + public String getSnippet() { + return mSnippet; + }获得用于显示的便签内容片段 + + public boolean isNoteType() { + return mType == Notes.TYPE_NOTE; + }//判断是否为便签类型 + + public void commit(boolean validateVersion) { + if (mIsCreate) { + if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) { + mDiffNoteValues.remove(NoteColumns.ID); + }//如果该ID是无效ID,并且存在这个ID,就将它移除 + + Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues);//内容解析器中插入便签的URL + try { + mId = Long.valueOf(uri.getPathSegments().get(1));//强制转换path为ID + } catch (NumberFormatException e) { + Log.e(TAG, "Get note id error :" + e.toString()); + throw new ActionFailureException("create note failed"); + }//捕获异常,转换出错,显示错误”获取note的ID出现错误“ + if (mId == 0) { + throw new IllegalStateException("Create thread id failed"); + }//若当前ID为0,则报错”创建ID失败 + + if (mType == Notes.TYPE_NOTE) { + for (SqlData sqlData : mDataList) { + sqlData.commit(mId, false, -1); + } + }//如果是便签类型,则使用sqldata中的commit函数进行保存 + } else { + if (mId <= 0 && mId != Notes.ID_ROOT_FOLDER && mId != Notes.ID_CALL_RECORD_FOLDER) { + Log.e(TAG, "No such note"); + throw new IllegalStateException("Try to update note with invalid id"); + }//判断是否含有这个便签 + if (mDiffNoteValues.size() > 0) { + mVersion ++; + int result = 0; + if (!validateVersion) { //如果是无效版本 + result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "(" + + NoteColumns.ID + "=?)", new String[] { + String.valueOf(mId) + }); + } else { //如果是有效版本 + result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "(" + + NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)", + new String[] { + String.valueOf(mId), String.valueOf(mVersion) + }); + } + if (result == 0) { + Log.w(TAG, "there is no update. maybe user updates note when syncing"); + }//如果内容解析器没有更新,那么报错:没有更新,或许用户在同步时进行更新 + } + + if (mType == Notes.TYPE_NOTE) { + for (SqlData sqlData : mDataList) { + sqlData.commit(mId, validateVersion, mVersion); + }//如果是note类型,引用sqldata的commit提交数据 + } + }//把当前修改保存到数据库 + + // refresh local info + loadFromCursor(mId); + if (mType == Notes.TYPE_NOTE) + loadDataContent(); + + mDiffNoteValues.clear(); + mIsCreate = false; + } +}//数据库中便签数据,方法包括读取便签内容,从数据库中获取便签数据,设置便签内容,提交便签到数据库 diff --git a/doc/吴亚婷精读部分/data/Task.java b/doc/吴亚婷精读部分/data/Task.java new file mode 100644 index 0000000..ecb14a8 --- /dev/null +++ b/doc/吴亚婷精读部分/data/Task.java @@ -0,0 +1,351 @@ +/* + * 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.gtask.data;//包名,说明依赖关系 + +import android.database.Cursor;//引用android的一些操作,下同 +import android.text.TextUtils; +import android.util.Log; + +import net.micode.notes.data.Notes;//引用本地包中的类,下同 +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.DataConstants; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.gtask.exception.ActionFailureException; +import net.micode.notes.tool.GTaskStringUtils; + +import org.json.JSONArray;//引用org.json的一些操作 +import org.json.JSONException; +import org.json.JSONObject; + + +public class Task extends Node { + private static final String TAG = Task.class.getSimpleName(); + + private boolean mCompleted;//是否完成 + + private String mNotes;//元数据信息 + + private JSONObject mMetaInfo;//在实例中存储数据的类型 + + private Task mPriorSibling;//对应优先兄弟类Task的指针 + + private TaskList mParent;//任务列表的指针 + + public Task() { + super(); + mCompleted = false; + mNotes = null; + mPriorSibling = null; + mParent = null; + mMetaInfo = null; + }//构造函数,进行初始化 + + public JSONObject getCreateAction(int actionId) { + JSONObject js = new JSONObject(); + + try { + // action_type + js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE); + + // action_id + js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); + + // index + js.put(GTaskStringUtils.GTASK_JSON_INDEX, mParent.getChildTaskIndex(this)); + + // entity_delta + JSONObject entity = new JSONObject();//创建实体数据并将name,创建者id,实体类型存入数据 + entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); + entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null"); + entity.put(GTaskStringUtils.GTASK_JSON_ENTITY_TYPE, + GTaskStringUtils.GTASK_JSON_TYPE_TASK); + if (getNotes() != null) { + entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes()); + }//如果存在notes,将其也放入entity中 + js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);//将entity变量(属于JSONObject类)作为一个数据存放进js变量中 + + // parent_id + js.put(GTaskStringUtils.GTASK_JSON_PARENT_ID, mParent.getGid()); + + // dest_parent_type + js.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT_TYPE, + GTaskStringUtils.GTASK_JSON_TYPE_GROUP); + + // list_id + js.put(GTaskStringUtils.GTASK_JSON_LIST_ID, mParent.getGid()); + + // prior_sibling_id + if (mPriorSibling != null) { + js.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, mPriorSibling.getGid()); + } + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("fail to generate task-create jsonobject"); + } + + return js; + }//获取创建的action及其属性值,获取与它相关联的任务列表的信息和兄弟action的信息 + + public JSONObject getUpdateAction(int actionId) { + JSONObject js = new JSONObject(); + + try { + // action_type + js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE); + + // action_id + js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); + + // id + js.put(GTaskStringUtils.GTASK_JSON_ID, getGid()); + + // entity_delta + JSONObject entity = new JSONObject(); + entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); + if (getNotes() != null) { + entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes()); + }//如果存在notes,将其也放入entity中 + entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted()); + js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("fail to generate task-update jsonobject"); + } + + return js; + }//接受更新的action + + public void setContentByRemoteJSON(JSONObject js) { + if (js != null) { + try { + // id + if (js.has(GTaskStringUtils.GTASK_JSON_ID)) { + setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID)); + } + + // last_modified + if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) { + setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)); + } + + // name + if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) { + setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME)); + } + + // notes + if (js.has(GTaskStringUtils.GTASK_JSON_NOTES)) { + setNotes(js.getString(GTaskStringUtils.GTASK_JSON_NOTES)); + } + + // deleted + if (js.has(GTaskStringUtils.GTASK_JSON_DELETED)) { + setDeleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_DELETED)); + } + + // completed + if (js.has(GTaskStringUtils.GTASK_JSON_COMPLETED)) { + setCompleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_COMPLETED)); + } + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("fail to get task content from jsonobject"); + } + } + }//通过云端JSONObiect获取任务内容 + + public void setContentByLocalJSON(JSONObject js) { + if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE) + || !js.has(GTaskStringUtils.META_HEAD_DATA)) { + Log.w(TAG, "setContentByLocalJSON: nothing is avaiable"); + }//判断传入的本地变量是否符合条件:1)如果传入的任务变量是空信息2)如果任务变量里没有head_note变量3)如果任务变量中没有head_data变量。那么反馈给用户出错信息 + + try { + JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); + JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA); + + if (note.getInt(NoteColumns.TYPE) != Notes.TYPE_NOTE) { + Log.e(TAG, "invalid type"); + return; + }//note类型匹配失败 + + for (int i = 0; i < dataArray.length(); i++) { + JSONObject data = dataArray.getJSONObject(i); + if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) { + setName(data.getString(DataColumns.CONTENT)); + break; + } + }//遍历 dataArray 查找与数据库中DataConstants.NOTE 记录信息一致的 data + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + } + }//通过本地JSONObject获取内容 + + public JSONObject getLocalJSONFromContent() { + String name = getName(); + try { + if (mMetaInfo == null) { + // new task created from web + if (name == null) { + Log.w(TAG, "the note seems to be an empty one"); + return null; + } + + JSONObject js = new JSONObject();//初始化四个指针 + JSONObject note = new JSONObject(); + JSONArray dataArray = new JSONArray(); + JSONObject data = new JSONObject(); + data.put(DataColumns.CONTENT, name); + dataArray.put(data); + js.put(GTaskStringUtils.META_HEAD_DATA, dataArray);//元数据类型信息非空,则进行同步更新操作 + note.put(NoteColumns.TYPE, Notes.TYPE_NOTE); + js.put(GTaskStringUtils.META_HEAD_NOTE, note);//同步任务 + return js; + } else { + // synced task + JSONObject note = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); + JSONArray dataArray = mMetaInfo.getJSONArray(GTaskStringUtils.META_HEAD_DATA); + + for (int i = 0; i < dataArray.length(); i++) { + JSONObject data = dataArray.getJSONObject(i); + if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) { + data.put(DataColumns.CONTENT, getName()); + break; + } + }//遍历 dataArray 查找与数据库中DataConstants.NOTE 记录信息一致的 data + + note.put(NoteColumns.TYPE, Notes.TYPE_NOTE); + return mMetaInfo; + } + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + return null; + } + }//从content机制获取本地JSON数据 + + public void setMetaInfo(MetaData metaData) { + if (metaData != null && metaData.getNotes() != null) { + try { + mMetaInfo = new JSONObject(metaData.getNotes()); + } catch (JSONException e) { + Log.w(TAG, e.toString()); + mMetaInfo = null; + } + }//如果元数据非空且其notes非空,则修改元数据类型信息 + }//设置元数据信息,若元数据不为空,则刷新当前元数据 + + public int getSyncAction(Cursor c) { + try { //异常处理,进行同步操作,如果不成功按照对应的情况进行异常信息的反馈,比如远端文档被删除、文档不匹配等等 + JSONObject noteInfo = null; + if (mMetaInfo != null && mMetaInfo.has(GTaskStringUtils.META_HEAD_NOTE)) { + noteInfo = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); + }//便签元数据已被删除,不存在,返回更新云端数据的同步行为 + + if (noteInfo == null) { + Log.w(TAG, "it seems that note meta has been deleted"); + return SYNC_ACTION_UPDATE_REMOTE; + }//云端便签 id 已被删除,不存在,返回更新本地数据的同步行为 + + if (!noteInfo.has(NoteColumns.ID)) { + Log.w(TAG, "remote note id seems to be deleted"); + return SYNC_ACTION_UPDATE_LOCAL; + }//便签 id 不匹配,返回更新本地数据的同步行为 + + // validate the note id now + if (c.getLong(SqlNote.ID_COLUMN) != noteInfo.getLong(NoteColumns.ID)) { + Log.w(TAG, "note id doesn't match"); + return SYNC_ACTION_UPDATE_LOCAL; + } + + if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) { + // there is no local update + if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { + // no update both side + return SYNC_ACTION_NONE; + } else { + // apply remote to local + return SYNC_ACTION_UPDATE_LOCAL; + } + } else { + // validate gtask id + if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) { + Log.e(TAG, "gtask id doesn't match"); + return SYNC_ACTION_ERROR; + } + if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { + // local modification only + return SYNC_ACTION_UPDATE_REMOTE; + } else { + return SYNC_ACTION_UPDATE_CONFLICT; + } + } + } catch (Exception e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + } + + return SYNC_ACTION_ERROR; + }通过Cursor获取同步操作 + + public boolean isWorthSaving() { + return mMetaInfo != null || (getName() != null && getName().trim().length() > 0) + || (getNotes() != null && getNotes().trim().length() > 0); + }//判断当前数据是否有效,若数据非空或名字合法存在且去除空格后的名字长度大于零则返回真值 + //以下函数基本都是完成数据反馈和状态判断的函数 + public void setCompleted(boolean completed) { + this.mCompleted = completed; + }//设定mCompleted + + public void setNotes(String notes) { + this.mNotes = notes; + }//设定mNotes + + public void setPriorSibling(Task priorSibling) { + this.mPriorSibling = priorSibling; + }//设置优先兄弟task的优先级 + + public void setParent(TaskList parent) { + this.mParent = parent; + }//设置父节点 + + public boolean getCompleted() { + return this.mCompleted; + }//获取task是否修改完毕的记录 + + public String getNotes() { + return this.mNotes; + }//获取mNotes的信息 + + public Task getPriorSibling() { + return this.mPriorSibling; + }//获取优先任务列表 + + public TaskList getParent() { + return this.mParent; + }//获取父节点列表 + +}//同步任务类的创建,继承父类Node diff --git a/doc/吴亚婷精读部分/data/TaskList.java b/doc/吴亚婷精读部分/data/TaskList.java new file mode 100644 index 0000000..96c3374 --- /dev/null +++ b/doc/吴亚婷精读部分/data/TaskList.java @@ -0,0 +1,346 @@ +/* + * 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.gtask.data;//包名,说明关系 + +import android.database.Cursor;//引用android的一些操作,下同 +import android.util.Log; + +import net.micode.notes.data.Notes;//引用本地包中的类,下同 +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.gtask.exception.ActionFailureException; +import net.micode.notes.tool.GTaskStringUtils; + +import org.json.JSONException;//引用org.json的一些操作 +import org.json.JSONObject; + +import java.util.ArrayList; + + +public class TaskList extends Node { + private static final String TAG = TaskList.class.getSimpleName(); + + private int mIndex;//当前TaskList的指针 + + private ArrayList mChildren;//类中主要用于保存数据的单元,用来实现一个以Task为元素的ArrayList + + public TaskList() { + super(); + mChildren = new ArrayList(); + mIndex = 1; + }//构造函数 + + public JSONObject getCreateAction(int actionId) { + JSONObject js = new JSONObject(); + + try { + // action_type + js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE); + + // action_id + js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); + + // index + js.put(GTaskStringUtils.GTASK_JSON_INDEX, mIndex); + + // entity_delta + JSONObject entity = new JSONObject(); + entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); + entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null"); + entity.put(GTaskStringUtils.GTASK_JSON_ENTITY_TYPE, + GTaskStringUtils.GTASK_JSON_TYPE_GROUP); + js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("fail to generate tasklist-create jsonobject"); + } + + return js; + }//生成并返回一个包含了一定数据的JSONObject实体,异常抛出和处理机制 + + public JSONObject getUpdateAction(int actionId) { + JSONObject js = new JSONObject(); + + try { + // action_type + js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE); + + // action_id + js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); + + // id + js.put(GTaskStringUtils.GTASK_JSON_ID, getGid()); + + // entity_delta + JSONObject entity = new JSONObject(); + entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); + entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted()); + js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("fail to generate tasklist-update jsonobject"); + } + + return js; + }//获取更新行为 + + public void setContentByRemoteJSON(JSONObject js) { + if (js != null) { + try { + // id + if (js.has(GTaskStringUtils.GTASK_JSON_ID)) { + setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID)); + } + + // last_modified + if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) { + setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)); + } + + // name + if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) { + setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME)); + } + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("fail to get tasklist content from jsonobject"); + } + } + }//获取云端数据库数据,设置列表内容 + + public void setContentByLocalJSON(JSONObject js) { + if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)) { + Log.w(TAG, "setContentByLocalJSON: nothing is avaiable"); + } + + try { + JSONObject folder = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); + + if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) { //若为一般文件夹,设置文件夹名 + String name = folder.getString(NoteColumns.SNIPPET); + setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + name); + } else if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) { //若为系统文件夹 + if (folder.getLong(NoteColumns.ID) == Notes.ID_ROOT_FOLDER) + setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT);//若为根目录文件夹,设置名称为MIUI系统文件夹前缀+默认文件夹名称 + else if (folder.getLong(NoteColumns.ID) == Notes.ID_CALL_RECORD_FOLDER) + setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + + GTaskStringUtils.FOLDER_CALL_NOTE);//若为通话记录文件夹,设置名称为MIUI系统文件夹前缀+通话便签文件夹名称 + else + Log.e(TAG, "invalid system folder");//无效系统文件夹警告 + } else { + Log.e(TAG, "error type"); + } + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + } + }//获取本地JSON数据设置对象js内容 + + public JSONObject getLocalJSONFromContent() { + try { + JSONObject js = new JSONObject(); + JSONObject folder = new JSONObject(); + + String folderName = getName();//获取文件夹名称 + if (getName().startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX)) + folderName = folderName.substring(GTaskStringUtils.MIUI_FOLDER_PREFFIX.length(), + folderName.length());//如果获取的文件夹名称是以[MIUI_Notes]开头,则文件夹名称应删掉前缀 + folder.put(NoteColumns.SNIPPET, folderName); + if (folderName.equals(GTaskStringUtils.FOLDER_DEFAULT) + || folderName.equals(GTaskStringUtils.FOLDER_CALL_NOTE)) + folder.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); + else + folder.put(NoteColumns.TYPE, Notes.TYPE_FOLDER); +//当获取的文件夹名称是以"Default"或"Call_Note开头,则为系统文件夹。否则为一般文件夹 + + js.put(GTaskStringUtils.META_HEAD_NOTE, folder); + + return js; + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + return null; + } + }//通过Content机制获取本地JSON数据 + + public int getSyncAction(Cursor c) { + try { + if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) { + // there is no local update + if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { + // no update both side + return SYNC_ACTION_NONE; + } else { + // apply remote to local + return SYNC_ACTION_UPDATE_LOCAL; + } + } else { + // validate gtask id + if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) { + Log.e(TAG, "gtask id doesn't match"); + return SYNC_ACTION_ERROR; + } + if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { + // local modification only + return SYNC_ACTION_UPDATE_REMOTE; + } else { + // for folder conflicts, just apply local modification + return SYNC_ACTION_UPDATE_REMOTE; + } + } + } catch (Exception e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + } + + return SYNC_ACTION_ERROR; + }//获取同步行为 + + public int getChildTaskCount() { + return mChildren.size(); + }//获取子任务数量 + + public boolean addChildTask(Task task) { + boolean ret = false; + if (task != null && !mChildren.contains(task)) { + //任务非空且任务表中不存在该任务 + ret = mChildren.add(task); + if (ret) { + // need to set prior sibling and parent + task.setPriorSibling(mChildren.isEmpty() ? null : mChildren + .get(mChildren.size() - 1)); + task.setParent(this); + } + } + return ret; + }//在当前的任务表末尾添加新的任务 + + public boolean addChildTask(Task task, int index) { + if (index < 0 || index > mChildren.size()) { + Log.e(TAG, "add child task: invalid index"); + return false; + }//若为无效索引,则报错:无效的索引导致添加子任务失败 + + int pos = mChildren.indexOf(task); + if (task != null && pos == -1) { + mChildren.add(index, task); + + // update the task list + Task preTask = null; + Task afterTask = null; + if (index != 0) + preTask = mChildren.get(index - 1); + if (index != mChildren.size() - 1) + afterTask = mChildren.get(index + 1); + + task.setPriorSibling(preTask); + if (afterTask != null) + afterTask.setPriorSibling(task); + } + + return true; + }//在当前任务表指定位置添加新的任务 + + public boolean removeChildTask(Task task) { + boolean ret = false; + int index = mChildren.indexOf(task); + if (index != -1) { + ret = mChildren.remove(task); + + if (ret) { + // reset prior sibling and parent + task.setPriorSibling(null); + task.setParent(null); + + // update the task list + if (index != mChildren.size()) { + mChildren.get(index).setPriorSibling( + index == 0 ? null : mChildren.get(index - 1)); + } + } + } + return ret; + }//删除任务表中的子任务 + + public boolean moveChildTask(Task task, int index) { + + if (index < 0 || index >= mChildren.size()) { + Log.e(TAG, "move child task: invalid index"); + return false; + } + + int pos = mChildren.indexOf(task); + if (pos == -1) { + Log.e(TAG, "move child task: the task should in the list"); + return false; + } + + if (pos == index) + return true; + return (removeChildTask(task) && addChildTask(task, index)); + }//将当前任务列表中的某个任务移动到指定位置index处 + + public Task findChildTaskByGid(String gid) { + for (int i = 0; i < mChildren.size(); i++) { + Task t = mChildren.get(i); + if (t.getGid().equals(gid)) { + return t; + } + } + return null; + }//通过gid寻找子任务 + + + public int getChildTaskIndex(Task task) { + return mChildren.indexOf(task); + }//获取指定子任务的索引 + + public Task getChildTaskByIndex(int index) { + if (index < 0 || index >= mChildren.size()) { + Log.e(TAG, "getTaskByIndex: invalid index"); + return null; + } + return mChildren.get(index); + }//通过索引获取子任务 + + public Task getChilTaskByGid(String gid) { + for (Task task : mChildren) { + if (task.getGid().equals(gid)) + return task; + } + return null; + }//返回指定gid的任务 + + public ArrayList getChildTaskList() { + return this.mChildren; + }//获取子任务列表 + + public void setIndex(int index) { + this.mIndex = index; + }//设置任务索引 + + public int getIndex() { + return this.mIndex; + }//获取任务索引 +}//本类继承于Node,将Task组织成同步任务列表进行管理 diff --git a/doc/吴亚婷精读部分/exception/ActionFailureException.java b/doc/吴亚婷精读部分/exception/ActionFailureException.java new file mode 100644 index 0000000..f4e0b81 --- /dev/null +++ b/doc/吴亚婷精读部分/exception/ActionFailureException.java @@ -0,0 +1,33 @@ +/* + * 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.gtask.exception;//小米便签行为异常处理 + +public class ActionFailureException extends RuntimeException { + private static final long serialVersionUID = 4425249765923293627L;//用于版本控制,验证版本一致性 + + public ActionFailureException() { + super(); + }//构造函数,调用父类的构造函数,以下两个同样 + + public ActionFailureException(String paramString) { + super(paramString); + } + + public ActionFailureException(String paramString, Throwable paramThrowable) { + super(paramString, paramThrowable); + } +}//异常处理类,继承自RuntimeException类 diff --git a/doc/吴亚婷精读部分/exception/NetworkFailureException.java b/doc/吴亚婷精读部分/exception/NetworkFailureException.java new file mode 100644 index 0000000..5457ee0 --- /dev/null +++ b/doc/吴亚婷精读部分/exception/NetworkFailureException.java @@ -0,0 +1,33 @@ +/* + * 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.gtask.exception;//小米便签网络异常处理 + +public class NetworkFailureException extends Exception { + private static final long serialVersionUID = 2107610287180234136L;//表明类的不同版本间的兼容性 + + public NetworkFailureException() { + super(); + }//构造函数,调用父类的构造函数,以下两个同样 + + public NetworkFailureException(String paramString) { + super(paramString); + } + + public NetworkFailureException(String paramString, Throwable paramThrowable) { + super(paramString, paramThrowable); + } +}//网络异常处理类,继承Exception类 diff --git a/doc/吴亚婷精读部分/remote/GTaskClient.java b/doc/吴亚婷精读部分/remote/GTaskClient.java new file mode 100644 index 0000000..7019d00 --- /dev/null +++ b/doc/吴亚婷精读部分/remote/GTaskClient.java @@ -0,0 +1,585 @@ +/* + * 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. + */ +//GTask客户端,提供登录Google账户,创建任务和任务列表,添加和删除结点,提交重置和更新,获取任务列表等功能 +package net.micode.notes.gtask.remote; + +import android.accounts.Account;//引用android的一些操作,下同 +import android.accounts.AccountManager; +import android.accounts.AccountManagerFuture; +import android.app.Activity; +import android.os.Bundle; +import android.text.TextUtils; +import android.util.Log; + +import net.micode.notes.gtask.data.Node;//引用本地包中的类 +import net.micode.notes.gtask.data.Task; +import net.micode.notes.gtask.data.TaskList; +import net.micode.notes.gtask.exception.ActionFailureException; +import net.micode.notes.gtask.exception.NetworkFailureException; +import net.micode.notes.tool.GTaskStringUtils; +import net.micode.notes.ui.NotesPreferenceActivity; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.cookie.Cookie; +import org.apache.http.impl.client.BasicCookieStore; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.params.BasicHttpParams; +import org.apache.http.params.HttpConnectionParams; +import org.apache.http.params.HttpParams; +import org.apache.http.params.HttpProtocolParams; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.LinkedList; +import java.util.List; +import java.util.zip.GZIPInputStream; +import java.util.zip.Inflater; +import java.util.zip.InflaterInputStream; + + +public class GTaskClient { + private static final String TAG = GTaskClient.class.getSimpleName(); + + private static final String GTASK_URL = "https://mail.google.com/tasks/";//Google邮箱的URL + + private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig";//获取的URL + + private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig";//上传的URL + + private static GTaskClient mInstance = null;//下列创建了客户端并且创建了客户端初始化需要用到的各种参数 + + private DefaultHttpClient mHttpClient;//网络地址客户端 + + private String mGetUrl; + + private String mPostUrl; + + private long mClientVersion; + + private boolean mLoggedin; + + private long mLastLoginTime; + + private int mActionId; + + private Account mAccount; + + private JSONArray mUpdateArray; + + private GTaskClient() { + mHttpClient = null; + mGetUrl = GTASK_GET_URL; + mPostUrl = GTASK_POST_URL; + mClientVersion = -1; + mLoggedin = false; + mLastLoginTime = 0; + mActionId = 1; + mAccount = null; + mUpdateArray = null; + }//构造函数 + + public static synchronized GTaskClient getInstance() { + if (mInstance == null) { + mInstance = new GTaskClient(); + } + return mInstance; + }//如果当前没有实例,则创建GTaskClient实例 + + public boolean login(Activity activity) { + // we suppose that the cookie would expire after 5 minutes + // then we need to re-login + final long interval = 1000 * 60 * 5; + if (mLastLoginTime + interval < System.currentTimeMillis()) { + mLoggedin = false; + } + + // need to re-login after account switch + if (mLoggedin + && !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity + .getSyncAccountName(activity))) { + mLoggedin = false; + } + + if (mLoggedin) { + Log.d(TAG, "already logged in"); + return true; + } + + mLastLoginTime = System.currentTimeMillis(); + String authToken = loginGoogleAccount(activity, false); + if (authToken == null) { + Log.e(TAG, "login google account failed"); + return false; + } + + // login with custom domain if necessary + if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase() + .endsWith("googlemail.com"))) { + StringBuilder url = new StringBuilder(GTASK_URL).append("a/"); + int index = mAccount.name.indexOf('@') + 1; + String suffix = mAccount.name.substring(index); + url.append(suffix + "/"); + mGetUrl = url.toString() + "ig"; + mPostUrl = url.toString() + "r/ig"; + + if (tryToLoginGtask(activity, authToken)) { + mLoggedin = true; + } + } + + // try to login with google official url + if (!mLoggedin) { + mGetUrl = GTASK_GET_URL; + mPostUrl = GTASK_POST_URL; + if (!tryToLoginGtask(activity, authToken)) { + return false; + } + } + + mLoggedin = true; + return true; + }//实现登录操作 + + private String loginGoogleAccount(Activity activity, boolean invalidateToken) { + String authToken;//使用登录令牌核实用户信息和判断登录状态 + AccountManager accountManager = AccountManager.get(activity);//给用户提供注册账号用的接口 + Account[] accounts = accountManager.getAccountsByType("com.google");//从谷歌获取用户名和密码的数组 + + if (accounts.length == 0) { + Log.e(TAG, "there is no available google account"); + return null; + }//没有获取到谷歌账号,输出日志信息“无有效的google账户” + + String accountName = NotesPreferenceActivity.getSyncAccountName(activity); + Account account = null; + for (Account a : accounts) { + if (a.name.equals(accountName)) { + account = a; + break; + } + } + if (account != null) { + mAccount = account; + } else { + Log.e(TAG, "unable to get an account with the same name in the settings"); + return null; + }//匹配活动的账户里是否存在账户,存在的话把这个账户记在mAccount中,没有的话显示不能在设置中获取相同名字的账户 + + // get the token now + AccountManagerFuture accountManagerFuture = accountManager.getAuthToken(account, + "goanna_mobile", null, activity, null, null); + try { + Bundle authTokenBundle = accountManagerFuture.getResult(); + authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN); + if (invalidateToken) { + accountManager.invalidateAuthToken("com.google", authToken); + loginGoogleAccount(activity, false); + } + } catch (Exception e) { + Log.e(TAG, "get auth token failed"); + authToken = null; + } + + return authToken; + }//登录Google的主函数,用于登录成功后获取认证令牌 + + private boolean tryToLoginGtask(Activity activity, String authToken) { + if (!loginGtask(authToken)) { + // maybe the auth token is out of date, now let's invalidate the + // token and try again + authToken = loginGoogleAccount(activity, true); + if (authToken == null) { + Log.e(TAG, "login google account failed"); + return false; + } + + if (!loginGtask(authToken)) { + Log.e(TAG, "login gtask failed"); + return false; + } + } + return true; + }//判断令牌对于登录GTask账号是否有效 + + private boolean loginGtask(String authToken) { + int timeoutConnection = 10000; + int timeoutSocket = 15000;//设定连接超时和端口超时的值分别为10秒和15秒 + HttpParams httpParameters = new BasicHttpParams();//实例化新的HTTP类 + HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);//设置连接超时的时间 + HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);//设置端口超时的时间 + mHttpClient = new DefaultHttpClient(httpParameters);//新建一个默认客户端 + BasicCookieStore localBasicCookieStore = new BasicCookieStore();//为cookie申请存储对象 + mHttpClient.setCookieStore(localBasicCookieStore);// 设置本地cookie + HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false); + + // login gtask + try { + String loginUrl = mGetUrl + "?auth=" + authToken; + HttpGet httpGet = new HttpGet(loginUrl); + HttpResponse response = null; + response = mHttpClient.execute(httpGet); + + // get the cookie now + List cookies = mHttpClient.getCookieStore().getCookies(); + boolean hasAuthCookie = false; + for (Cookie cookie : cookies) { + if (cookie.getName().contains("GTL")) { + hasAuthCookie = true; + } + } + if (!hasAuthCookie) { + Log.w(TAG, "it seems that there is no auth cookie"); + } + + // get the client version + String resString = getResponseContent(response.getEntity()); + String jsBegin = "_setup("; + String jsEnd = ")}"; + int begin = resString.indexOf(jsBegin); + int end = resString.lastIndexOf(jsEnd); + String jsString = null; + if (begin != -1 && end != -1 && begin < end) { + jsString = resString.substring(begin + jsBegin.length(), end); + } + JSONObject js = new JSONObject(jsString); + mClientVersion = js.getLong("v"); + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + return false; + } catch (Exception e) { + // simply catch all exceptions + Log.e(TAG, "httpget gtask_url failed"); + return false; + } + + return true; + }//实现登录Gtask的方法 + + private int getActionId() { + return mActionId++; + }//获取活动ID加1并将其返回 + + private HttpPost createHttpPost() { + HttpPost httpPost = new HttpPost(mPostUrl); + httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); + httpPost.setHeader("AT", "1"); + return httpPost; + }//创建一个httpPost对象,用于向网络传输数据 + + private String getResponseContent(HttpEntity entity) throws IOException { + String contentEncoding = null; + if (entity.getContentEncoding() != null) { + contentEncoding = entity.getContentEncoding().getValue(); + Log.d(TAG, "encoding: " + contentEncoding); + }//若获取的内容编码不为空,给contentEncoding赋值,打印日志“encoding:内容编码” + + InputStream input = entity.getContent(); + if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) { + input = new GZIPInputStream(entity.getContent()); + } else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) { + Inflater inflater = new Inflater(true); + input = new InflaterInputStream(entity.getContent(), inflater); + } + + try { + InputStreamReader isr = new InputStreamReader(input);//InputStreamReader类:是从字节流到字符流的桥接器,它使用指定的字符集读取字节并将它们解码为字符 + BufferedReader br = new BufferedReader(isr);//BufferedReader类:缓存读取类,用于快速的读缓存操作 + StringBuilder sb = new StringBuilder(); + + while (true) { + String buff = br.readLine(); + if (buff == null) { + return sb.toString(); + } + sb = sb.append(buff); + }//将BufferedReader类br的内容逐行读取并存储StringBuilder类的sb中。然后返回sb的string格式。 + } finally { + input.close(); + } + }//获取网络上的数据和资源 + + private JSONObject postRequest(JSONObject js) throws NetworkFailureException { + if (!mLoggedin) { + Log.e(TAG, "please login first"); + throw new ActionFailureException("not logged in"); + }//用户未登录,输出提示信息 + + HttpPost httpPost = createHttpPost();//实例化一个HttpPost对象向服务器传输数据 + try { + LinkedList list = new LinkedList();//LinkedList 类:是一个继承于AbstractSequentialList的双向链表 + list.add(new BasicNameValuePair("r", js.toString())); + UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");//普通的键值对"UTF-8" + httpPost.setEntity(entity); + + // execute the post + HttpResponse response = mHttpClient.execute(httpPost); + String jsString = getResponseContent(response.getEntity()); + return new JSONObject(jsString); + + } catch (ClientProtocolException e) { //接下来是对四种不同的异常出现时的处理 + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new NetworkFailureException("postRequest failed"); + } catch (IOException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new NetworkFailureException("postRequest failed"); + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("unable to convert response content to jsonobject"); + } catch (Exception e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("error occurs when posting request"); + } + }//通过JSON向客户端发送请求并调用getResponseContent方法来获取返回的信息 + + public void createTask(Task task) throws NetworkFailureException { + commitUpdate(); + try { + JSONObject jsPost = new JSONObject();//利用JSON获取Task中的内容,并创建相应的jspost + JSONArray actionList = new JSONArray(); + + // action_list + actionList.put(task.getCreateAction(getActionId())); + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); + + // client_version + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + // post + JSONObject jsResponse = postRequest(jsPost); + JSONObject jsResult = (JSONObject) jsResponse.getJSONArray( + GTaskStringUtils.GTASK_JSON_RESULTS).get(0); + task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("create task: handing jsonobject failed"); + } + }//创建单个任务,包含活动列表,客户端版本,发布请求等 + + public void createTaskList(TaskList tasklist) throws NetworkFailureException { + commitUpdate(); + try { + JSONObject jsPost = new JSONObject();//利用JSON获取Task中的内容,并创建相应的jspost + JSONArray actionList = new JSONArray(); + + // action_list + actionList.put(tasklist.getCreateAction(getActionId())); + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); + + // client version + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + // post + JSONObject jsResponse = postRequest(jsPost); + JSONObject jsResult = (JSONObject) jsResponse.getJSONArray( + GTaskStringUtils.GTASK_JSON_RESULTS).get(0); + tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("create tasklist: handing jsonobject failed"); + } + }//创建任务列表,最后设置的是tasklist的gid + + public void commitUpdate() throws NetworkFailureException { + if (mUpdateArray != null) { + try { + JSONObject jsPost = new JSONObject(); + + // action_list + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray); + + // client_version + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + postRequest(jsPost); + mUpdateArray = null; + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("commit update: handing jsonobject failed"); + } + } + }//提交更新数据 + + public void addUpdateNode(Node node) throws NetworkFailureException { + if (node != null) { + // too many update items may result in an error + // set max to 10 items + if (mUpdateArray != null && mUpdateArray.length() > 10) { + commitUpdate(); + } + + if (mUpdateArray == null) + mUpdateArray = new JSONArray(); + mUpdateArray.put(node.getUpdateAction(getActionId())); + } + }//添加更新的节点,对更新列表进行操作 + + public void moveTask(Task task, TaskList preParent, TaskList curParent) + throws NetworkFailureException { + commitUpdate(); + try { + JSONObject jsPost = new JSONObject(); + JSONArray actionList = new JSONArray(); + JSONObject action = new JSONObject(); + + // action_list + action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE); + action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); + action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid()); + if (preParent == curParent && task.getPriorSibling() != null) { + // put prioring_sibing_id only if moving within the tasklist and + // it is not the first one + action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling()); + } + action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid()); + action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid()); + if (preParent != curParent) { + // put the dest_list only if moving between tasklists + action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid()); + } + actionList.put(action); + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); + + // client_version + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + postRequest(jsPost); + + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("move task: handing jsonobject failed"); + } + }//移动任务到不同的task列表中 + + public void deleteNode(Node node) throws NetworkFailureException { + commitUpdate(); + try { + JSONObject jsPost = new JSONObject(); + JSONArray actionList = new JSONArray(); + + // action_list + node.setDeleted(true); + actionList.put(node.getUpdateAction(getActionId())); + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); + + // client_version + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + postRequest(jsPost); + mUpdateArray = null; + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("delete node: handing jsonobject failed"); + } + }//删除单个节点 + + public JSONArray getTaskLists() throws NetworkFailureException { + if (!mLoggedin) { + Log.e(TAG, "please login first"); + throw new ActionFailureException("not logged in"); + }//检查登录状态 + + try { + HttpGet httpGet = new HttpGet(mGetUrl); + HttpResponse response = null; + response = mHttpClient.execute(httpGet); + + // get the task list + String resString = getResponseContent(response.getEntity()); + String jsBegin = "_setup("; + String jsEnd = ")}"; + int begin = resString.indexOf(jsBegin); + int end = resString.lastIndexOf(jsEnd); + String jsString = null; + if (begin != -1 && end != -1 && begin < end) { + jsString = resString.substring(begin + jsBegin.length(), end); + } + JSONObject js = new JSONObject(jsString); + return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS); + } catch (ClientProtocolException e) { //对可能出现的三种异常进行处理 + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new NetworkFailureException("gettasklists: httpget failed"); + } catch (IOException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new NetworkFailureException("gettasklists: httpget failed"); + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("get task lists: handing jasonobject failed"); + } + }//获取任务列表 + + public JSONArray getTaskList(String listGid) throws NetworkFailureException { + commitUpdate(); + try { + JSONObject jsPost = new JSONObject(); + JSONArray actionList = new JSONArray(); + JSONObject action = new JSONObject(); + + // action_list + action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL); + action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); + action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid); + action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false); + actionList.put(action); + jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); + + // client_version + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + + JSONObject jsResponse = postRequest(jsPost); + return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS); + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + throw new ActionFailureException("get task list: handing jsonobject failed"); + } + }//对于已经获取的任务列表,通过ID来准确获取到单个任务 + + public Account getSyncAccount() { + return mAccount; + }//获取同步账户 + + public void resetUpdateArray() { + mUpdateArray = null; + }//重置更新内容 +}//GTaskClient类:实现GTask的登录以及创建GTask任务和任务列表,从网络上获取任务内容 From 587550235d644d42e9bb8117294bd3d71f3e9964 Mon Sep 17 00:00:00 2001 From: jxp <1962998487@qq.com> Date: Wed, 12 Apr 2023 16:34:53 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E8=92=8B=E6=AC=A3=E8=90=8D=E7=B2=BE?= =?UTF-8?q?=E8=AF=BB=E9=83=A8=E5=88=86=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/蒋欣萍精读部分/BackupUtils.java | 345 ++++++++++++++++ doc/蒋欣萍精读部分/DataUtils.java | 295 ++++++++++++++ .../GTaskStringUtils.java | 112 ++++++ doc/蒋欣萍精读部分/Note.java | 253 ++++++++++++ doc/蒋欣萍精读部分/NoteItemData.java | 224 +++++++++++ doc/蒋欣萍精读部分/ResourceParser.java | 181 +++++++++ doc/蒋欣萍精读部分/WorkingNote.java | 370 ++++++++++++++++++ 7 files changed, 1780 insertions(+) create mode 100644 doc/蒋欣萍精读部分/BackupUtils.java create mode 100644 doc/蒋欣萍精读部分/DataUtils.java create mode 100644 doc/蒋欣萍精读部分/GTaskStringUtils.java create mode 100644 doc/蒋欣萍精读部分/Note.java create mode 100644 doc/蒋欣萍精读部分/NoteItemData.java create mode 100644 doc/蒋欣萍精读部分/ResourceParser.java create mode 100644 doc/蒋欣萍精读部分/WorkingNote.java diff --git a/doc/蒋欣萍精读部分/BackupUtils.java b/doc/蒋欣萍精读部分/BackupUtils.java new file mode 100644 index 0000000..802aeab --- /dev/null +++ b/doc/蒋欣萍精读部分/BackupUtils.java @@ -0,0 +1,345 @@ +/* + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) //打开网站 + * + * Licensed under the Apache License, Version 2.0 (the "License"); //2.0版本 + * 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. //BackupUtils 是一个备份工具类,用于数据备份读取和显示 + */ + +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 net.micode.notes.R; // 从Notes项目的其他软件包中调用类。 +import net.micode.notes.data.Notes; //数据便签类 +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.DataConstants; // 数据类型类 +import net.micode.notes.data.Notes.NoteColumns; //便签类 + +import java.io.File; //引入java的文件处理,输入输出等功能 +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"; //实例化化对象BackupUtils + // Singleton stuff + private static BackupUtils sInstance; //实例化一个BackupUnils的对象sInstance + + public static synchronized BackupUtils getInstance(Context context) { //定义一个synchronize的函数,每次运行到synchronize时,首先判断这个函数是否有别人在用这个方法,如果有就等完再用,否则即可调用此函数 + if (sInstance == null) { //如果当前备份不存在,则声明一个 + sInstance = new BackupUtils(context); + } + return sInstance; //返回当前sInstance的值(或备份文件) + } + + /** + * Following states are signs to represents backup or restore //以下状态用于表示备份或者恢复 + + * status //定义状态常量 + */ + // Currently, the sdcard is not mounted + public static final int STATE_SD_CARD_UNMOUONTED = 0; //备份文件夹不存在 + // The backup file not exist + public static final int STATE_BACKUP_FILE_NOT_EXIST = 1; //数据已被破坏,可能被修改 + // The data is not well formated, may be changed by other programs + public static final int STATE_DATA_DESTROIED = 2; //系统出现错误 + // Some run-time exception which causes restore or backup fails + public static final int STATE_SYSTEM_ERROR = 3; //导出成功 + // Backup or restore success + public static final int STATE_SUCCESS = 4; //成功导出/状态正常 + + private TextExport mTextExport; //实例化一个textexport的对象 + + 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 { //定义了一个TextExport的类,包含笔记ID、修改日期、数据及其格式类型 + private static final String[] NOTE_PROJECTION = { //定义了一个数组储存便签的信息 + NoteColumns.ID, + NoteColumns.MODIFIED_DATE, + NoteColumns.SNIPPET, + NoteColumns.TYPE + }; + + private static final int NOTE_COLUMN_ID = 0; //初始化标识:笔记ID标识为0;笔记的修改日期标识为1;笔记的数据标识为2. + + private static final int NOTE_COLUMN_MODIFIED_DATE = 1; + + private static final int NOTE_COLUMN_SNIPPET = 2; + + private static final String[] DATA_PROJECTION = { //定义字符串存储数据的基本信息 + DataColumns.CONTENT, + DataColumns.MIME_TYPE, + DataColumns.DATA1, + DataColumns.DATA2, + DataColumns.DATA3, + DataColumns.DATA4, + }; + + private static final int DATA_COLUMN_CONTENT = 0; //标识设定:数据内容标识为0;媒体类型标识为1;访问日期标识为2;电话号码标识为4 + + 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; //文档格式标识:名称赋值为0,日期赋值为1,内容赋值为2 + 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) { //从context类实例中获取信息,给对应的属性赋初始值 + TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); + mContext = context; //定义文件内容为便签内容 + mFileName = ""; //文件名初始化为空 + mFileDirectory = ""; //文件路径初始化为空 + } + + private String getFormat(int id) { //获取id对应的格式 + return TEXT_FORMAT[id]; + } + + /** + * Export the folder identified by folder id to text //输出文件夹 + */ + private void exportFolderToText(String folderId, PrintStream ps) { //通过查询parent id是文件夹id的note来选出制定ID文件夹下的Note + // Query notes belong to this folder + Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, //根据便签目录的URI找到利用的数据,并根据PARENT_ID和文件ID在数据里查找,并放到游标中 + NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] { + folderId + }, null); //如果查询结果非空那么按照时间格式输出最后修改的时间 + + if (notesCursor != null) { //将查找到的数据全部导到文本里面 + if (notesCursor.moveToFirst()) { // 如果游标执行到第一行 + do { //ps里面保存有这份note的日期 + // Print note's last modified date + ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( //打印最后修改日期 + mContext.getString(R.string.format_datetime_mdhm), + notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); + // Query data belong to this note + String noteId = notesCursor.getString(NOTE_COLUMN_ID); //查询属于该便签的数据 + exportNoteToText(noteId, ps); //通过文件的标识将目录导出成文件 + } while (notesCursor.moveToNext()); //遍历这个文件夹底下所有的便签 + } + notesCursor.close(); //关闭游标 + } + } + + /** + * Export note identified by id to a print stream + */ + private void exportNoteToText(String noteId, PrintStream ps) { //打印便签的内容,分为电话号码类型和便签类型 + Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, + DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { //将CONTENT_DATA_URI里DATA_PROFECTION内容扫描到游标中,按默认顺序进行排序 + noteId + }, null); + + if (dataCursor != null) { //如果数据游标不为空 + if (dataCursor.moveToFirst()) { + do { + String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); // 获得便签的媒体类型 + if (DataConstants.CALL_NOTE.equals(mimeType)) { //判断便签的内容,如果是电话记录,那么在这一个代码块内打印 + // Print phone number + String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); //获取手机号码 + long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); // 获取通话时长 + String location = dataCursor.getString(DATA_COLUMN_CONTENT); //获取地址 + + if (!TextUtils.isEmpty(phoneNumber)) { //判断是否是空字符 + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), //输出电话号码phoneNumber + phoneNumber)); + } + // Print call date + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat //输出calldate + .format(mContext.getString(R.string.format_datetime_mdhm), + callDate))); + // Print call attachment location + if (!TextUtils.isEmpty(location)) { //判断是否存在位置信息 + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), //打印便签格式和位置信息 + location)); + } + } else if (DataConstants.NOTE.equals(mimeType)) { //如果是便签的内容,那么直接输出便签里面包含的东西 + String content = dataCursor.getString(DATA_COLUMN_CONTENT); + if (!TextUtils.isEmpty(content)) { //如果便签内容不为空值 + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), //判断文本包中内容是否为空,若非空则将文本内容输出到打印输出流中 + content)); + } + } + } while (dataCursor.moveToNext()); //光标下移 + } + dataCursor.close(); //关闭光标 + } + // print a line separator between note + try { + ps.write(new byte[] { //在note下面输出一条线 + Character.LINE_SEPARATOR, Character.LETTER_NUMBER + }); + } catch (IOException e) { //检测异常,如果有异常输出红色TAG + Log.e(TAG, e.toString()); //捕获错误信息并记录异常日志 + } + } + + /** + * Note will be exported as text which is user readable + */ + public int exportToText() { //总函数,调用上面的exportFolder和exportNote + if (!externalStorageAvailable()) { //检查外部设备安装情况,如果未安装好,则蓝色字体输出debug信息,并返回STATE_SD_CARD_UNMOUNOTED值为0 + 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"); //如果ps赋值失败,则输出显示错误的信息,并返回状态错误信息 + return STATE_SYSTEM_ERROR; + } //导出文件夹及其中的便签 + // First export folder and its notes + Cursor folderCursor = mContext.getContentResolver().query( //定位需要导出的文件夹 + Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, + "(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " + + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " + + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null); + + if (folderCursor != null) { //文件夹游标不为空,即存在文件夹 + if (folderCursor.moveToFirst()) { //文件夹光标在第一行时 + do { //打印输出文件夹的名称 + // Print folder's name + String folderName = ""; //输出文件夹的名字 + if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { // 根据folder在数据库中的位置来找到文件名 + folderName = mContext.getString(R.string.call_record_folder_name); + } else { + folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); //若未得到文件夹位置,则从便签前面一部分字符串为文件夹名称 + } + if (!TextUtils.isEmpty(folderName)) { //判断folderName是否存在,存在即输出其格式和flodername + ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); + } + String folderId = folderCursor.getString(NOTE_COLUMN_ID); //通过便签id得到folderID + exportFolderToText(folderId, ps); //将文件夹中内容导出打印 + } while (folderCursor.moveToNext()); //文件夹的光标下移 + } + folderCursor.close(); //关闭游标 + } + + // Export notes in root's folder + Cursor noteCursor = mContext.getContentResolver().query( //输出根目录下的便签 + Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, + NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID + + "=0", null, null); + + if (noteCursor != null) { //便签对应游标不为空 + if (noteCursor.moveToFirst()) { //便签光标下移 + do { + ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( //打印便签的修改时间 + mContext.getString(R.string.format_datetime_mdhm), + noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); //访问便签中的数据的相关信息 + // Query data belong to this note + String noteId = noteCursor.getString(NOTE_COLUMN_ID); //找到这块数据的ID. + exportNoteToText(noteId, ps); //将便签以文本的形式导出并打印 + } while (noteCursor.moveToNext()); // 便签光标下移 + } + noteCursor.close(); //关闭游标 + } + ps.close(); + + return STATE_SUCCESS; //返回成功导出的状态 + } + + /** + * Get a print stream pointed to the file {@generateExportedTextFile} + */ + private PrintStream getExportToTextPrintStream() { //将要输出的内容整合到一个输出里并返回该输出流 + File file = generateFileMountedOnSDcard(mContext, R.string.file_path, //初始化存储在SD卡的文件 + 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; //初始化ps + try { + FileOutputStream fos = new FileOutputStream(file); //将ps输出流输出到特定的文件,目的就是导出到文件,而不是直接输出 + ps = new PrintStream(fos); + } catch (FileNotFoundException e) { //找不到系统指定文件 + e.printStackTrace(); + return null; + } catch (NullPointerException e) { //捕获空指针异常 + e.printStackTrace(); + return null; + } + return ps; + } + } + + /** + * Generate the text file to store imported data + */ + 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; //try catch 异常处理 + } +} + + diff --git a/doc/蒋欣萍精读部分/DataUtils.java b/doc/蒋欣萍精读部分/DataUtils.java new file mode 100644 index 0000000..c7d8eaf --- /dev/null +++ b/doc/蒋欣萍精读部分/DataUtils.java @@ -0,0 +1,295 @@ +/* + * 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; //导入需要的包 + + +public class DataUtils { //定义数据工具类 + public static final String TAG = "DataUtils"; + public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { //实现了批量删除便签 + if (ids == null) { //判断是否存在笔记 + Log.d(TAG, "the ids is null"); + return true; + } + if (ids.size() == 0) { //判断笔记内容是否为空 + Log.d(TAG, "no id is in the hashset"); //打印结果到日志里面 ,并进行存储 + return true; + } + + ArrayList operationList = new ArrayList(); //创建一个新的数组列表 + for (long id : ids) { //遍历数据,如果此数据为根目录则跳过此数据不删除,如果不是根目录则将此数据删除 + if(id == Notes.ID_ROOT_FOLDER) { //避免出现删除系统目录的情况 + Log.e(TAG, "Don't delete system folder root"); //检测当前id是不是指向根目录,若是返回一个异常信息 + continue; //如果发现是根文件夹,则不删除 + } + ContentProviderOperation.Builder builder = ContentProviderOperation //使用newdelete进行删除 + .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + operationList.add(builder.build()); //将操作添加至列表 + } + try { //返回被删除的数据,如果返回为空则删除失败返回false,打印异常信息,删除成功返回true + ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); //主机名Authority用于唯一标识这个ContentProvider,外部调用者可以根据这个标识来找到它。 + if (results == null || results.length == 0 || results[0] == null) { + Log.d(TAG, "delete notes failed, ids:" + ids.toString()); //检测当前要删除的便签是否是根目录,如果是根目录,,返回异常信息 + return false; + } + return true; + } catch (RemoteException e) { //对错误进行处理 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } catch (OperationApplicationException e) { + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } + return false; + } + + public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) { //将便签移动到文件夹中 + ContentValues values = new ContentValues(); //实例化一个contentValues类 + values.put(NoteColumns.PARENT_ID, desFolderId); //将当前ID更改为移动的目录ID + values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); //设置origin也即原本的父节点为原本的文件夹的id + values.put(NoteColumns.LOCAL_MODIFIED, 1); //是否修改状态置为1 + resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); //对需要移动的便签进行数据更新,然后用update实现 + } + + public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, //批量的将标签移动到另一个目录下 + long folderId) { + if (ids == null) { // 判断便签ID是否为空 + Log.d(TAG, "the ids is null"); + return true; + } + + ArrayList operationList = new ArrayList(); //将ids里包含的每一列的数据逐次加入到operationList中,等待最后的批量处理 + for (long id : ids) { //遍历所有选中的便签的id + ContentProviderOperation.Builder builder = ContentProviderOperation //通过withAppendedId方法,为该Uri加上ID + .newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + builder.withValue(NoteColumns.PARENT_ID, folderId); //修改便签的父节点ID为新的文件夹的ID + builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); // 更改状态置为1 + operationList.add(builder.build()); //将移动的操作添加到操作列表 + } + + try { //对异常进行处理与汇报的容错机制 + ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); //使用函数applyBatch一次性处理一个操作列表 + if (results == null || results.length == 0 || results[0] == null) { + Log.d(TAG, "delete notes failed, ids:" + ids.toString()); + return false; + } + return true; + } catch (RemoteException e) { //异常处理 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } catch (OperationApplicationException e) { // 异常处理 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } + return false; + } + + /** + * Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}} + */ + public static int getUserFolderCount(ContentResolver resolver) { //获取用户文件夹数 + Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI, //数据库操作,拼接字符串使其成为数据库指令 + new String[] { "COUNT(*)" }, + NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?", + new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)}, //String.valueof将形参转成字符串返回 + null); + + int count = 0; + if(cursor != null) { //尝试得到用户文件夹的数量 + if(cursor.moveToFirst()) { + try { // 异常处理 + count = cursor.getInt(0); + } catch (IndexOutOfBoundsException e) { // 索引序号超出界限 + Log.e(TAG, "get folder count failed:" + e.toString()); + } finally { + cursor.close(); //关闭游标 + } + } + } + return count; + } + + public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) { //判断该便签在数据库中是否可以查看到 + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), //通过withappendedid的方法为uri加上id + null, + NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER, // sql语句,表示筛选出列中type等于string数组中type,且每一项的PARENT_ID不等于Notes.ID.TRAXH_FOLDER + new String [] {String.valueOf(type)}, + null); // 查询条件:type符合,且不属于垃圾文件夹 + + boolean exist = false; //判断note是否在数据库中,并返回相应的布尔值 + if (cursor != null) { //用getcount函数判断cursor是否为空 + if (cursor.getCount() > 0) { + exist = true; //判断是否存在于数据库 + } + cursor.close(); //关闭游标 + } + return exist; //返回是否有便签的状态 + } + + public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { //判断该note是否在数据库中存在 + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), + null, null, null, null); + + boolean exist = false; //初始化存在状态 + if (cursor != null) { //根据getcount此时的值可以判断dataID的存在性 + if (cursor.getCount() > 0) { //根据筛选出来的条数判断存在性 + exist = true; + } + cursor.close(); //关闭游标 + } + return exist; + } + + public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { //通过名字查询是否在可见文件夹中 + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), //通过URI与dataId在数据库中查找数据 + null, null, null, null); + + boolean exist = false; //根据数据的有无返回相应的布尔值 + if (cursor != null) { + if (cursor.getCount() > 0) { //调用对应的uri的数据值进行查询 + exist = true; + } + cursor.close(); + } + return exist; + } + + public static boolean checkVisibleFolderName(ContentResolver resolver, String name) { //通过名字检查可见文件夹是否存在 + Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null, //筛选出type相同,并且未被删除,名字对的上的 + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + + " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + + " AND " + NoteColumns.SNIPPET + "=?", + new String[] { name }, null); //通过名字查找文件文件 + boolean exist = false; + if(cursor != null) { //判断找到的文件名返回文件是否存在的bool值 + if(cursor.getCount() > 0) { + exist = true; + } + cursor.close(); + } + return exist; + } + + public static HashSet getFolderNoteWidget(ContentResolver resolver, long folderId) { //获取便签的小部件 + Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, //查询条件:父id为传入的文件夹id + new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, + NoteColumns.PARENT_ID + "=?", + new String[] { String.valueOf(folderId) }, + null); + + HashSet set = null; //根据窗口的记录一一添加对应的属性值 + if (c != null) { //如果为null,说明该文件夹没有窗口,直接返回空即可 + if (c.moveToFirst()) { + set = new HashSet(); + do { + try { //把每一个条目对应的窗口id和type记录下来,放到set里面。每一行的第0个int和第1个int分别对应widgetId和widgetType + AppWidgetAttribute widget = new AppWidgetAttribute(); + widget.widgetId = c.getInt(0); //0对应的NoteColumns.WIDGET_ID + widget.widgetType = c.getInt(1); //1对应的NoteColumns.WIDGET_TYPE + set.add(widget); + } catch (IndexOutOfBoundsException e) { //捕捉序号越界错误,发送红色错误信息 + Log.e(TAG, e.toString()); + } //继续查询下一条 + } while (c.moveToNext()); + } + c.close(); + } + return set; + } + + public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { //通过笔记ID获取号码 + Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, + new String [] { CallNote.PHONE_NUMBER }, + CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?", + new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE }, //新建字符列表 + null); + + if (cursor != null && cursor.moveToFirst()) { //获取到电话号码并处理异常 + try { + return cursor.getString(0); //返回电话号码 + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "Get call number fails " + e.toString()); + } finally { + cursor.close(); + } + } + return ""; + } + + public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { //获取ID通过电话号码和呼叫日期 + Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, //通过数据库操作,查询条件是(callDate和phoneNumber匹配传入参数的值) + new String [] { CallNote.NOTE_ID }, + CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL(" + + CallNote.PHONE_NUMBER + ",?)", + new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber }, + null); + + if (cursor != null) { //得到该note的系统属性,并以Long值的形式来保存 + if (cursor.moveToFirst()) { + try { + return cursor.getLong(0); //0对应的CallNote.NOTE_ID + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "Get call note id fails " + e.toString()); + } + } + cursor.close(); + } + return 0; + } + + public static String getSnippetById(ContentResolver resolver, long noteId) { //通过ID搜索代码 + Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, //通过ID查询 + new String [] { NoteColumns.SNIPPET }, + NoteColumns.ID + "=?", + new String [] { String.valueOf(noteId)}, + null); //通过数据库操作,查询条件是(callDate和phoneNumber匹配传入参数的值 + + if (cursor != null) { //以string的形式获取该对象当前行指定列的值 + String snippet = ""; //对字符串进行格式处理,将字符串两头的空格去掉同时将换行符去掉 + if (cursor.moveToFirst()) { + snippet = cursor.getString(0); + } + cursor.close(); + return snippet; + } + throw new IllegalArgumentException("Note is not found with id: " + noteId); //IllegalArgumentException即非法传参异常,传的参数类型冲突 + } + + public static String getFormattedSnippet(String snippet) { //把文件名标准化 + if (snippet != null) { + snippet = snippet.trim(); //trim()函数: 移除字符串两侧的空白字符或其他预定义字符 + int index = snippet.indexOf('\n'); + if (index != -1) { + snippet = snippet.substring(0, index); //截取到第一个换行符 + } + } + return snippet; + } +} diff --git a/doc/蒋欣萍精读部分/GTaskStringUtils.java b/doc/蒋欣萍精读部分/GTaskStringUtils.java new file mode 100644 index 0000000..c614485 --- /dev/null +++ b/doc/蒋欣萍精读部分/GTaskStringUtils.java @@ -0,0 +1,112 @@ +/* + * 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. + */ + //定义了很多的静态字符串,目的就是为了提供jsonObject中相应字符串的"key"。把这些静态的定义单独写到了一个类里面 +package net.micode.notes.tool; + +public class GTaskStringUtils { //定义了一堆静态字符常量,方便以后在使用的时候直接调用 + public final static String GTASK_JSON_ACTION_ID = "action_id"; //行动ID + + public final static String GTASK_JSON_ACTION_LIST = "action_list"; //任务列表 + + public final static String GTASK_JSON_ACTION_TYPE = "action_type"; //任务类型 + + public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; //新建 + + public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; + + public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; //移动 + + public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; + + public final static String GTASK_JSON_CREATOR_ID = "creator_id"; + + public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; //子实体 + + public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; //客户端 + + public final static String GTASK_JSON_COMPLETED = "completed"; + + public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; //当前列表位置 + + public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; //失败 + + public final static String GTASK_JSON_DELETED = "deleted"; // 删除 + + public final static String GTASK_JSON_DEST_LIST = "dest_list"; + + public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; + + public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type"; + + public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; + + public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; + + public final static String GTASK_JSON_GET_DELETED = "get_deleted"; + + public final static String GTASK_JSON_ID = "id"; + + public final static String GTASK_JSON_INDEX = "index"; //索引 + + public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; + + public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point"; + + public final static String GTASK_JSON_LIST_ID = "list_id"; + + public final static String GTASK_JSON_LISTS = "lists"; + + public final static String GTASK_JSON_NAME = "name"; + + public final static String GTASK_JSON_NEW_ID = "new_id"; + + public final static String GTASK_JSON_NOTES = "notes"; + + public final static String GTASK_JSON_PARENT_ID = "parent_id"; //搭档ID + + public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id"; + + public final static String GTASK_JSON_RESULTS = "results"; + + public final static String GTASK_JSON_SOURCE_LIST = "source_list"; + + public final static String GTASK_JSON_TASKS = "tasks"; //任务栏 + + public final static String GTASK_JSON_TYPE = "type"; + + public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; + + public final static String GTASK_JSON_TYPE_TASK = "TASK"; //任务 + + public final static String GTASK_JSON_USER = "user"; + + public final static String MIUI_FOLDER_PREFFIX = "[MIUI_Notes]"; + + public final static String FOLDER_DEFAULT = "Default"; + + public final static String FOLDER_CALL_NOTE = "Call_Note"; //呼叫小米便签 + + public final static String FOLDER_META = "METADATA"; + + public final static String META_HEAD_GTASK_ID = "meta_gid"; + + public final static String META_HEAD_NOTE = "meta_note"; + + public final static String META_HEAD_DATA = "meta_data"; //数据 + + public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE"; + +} diff --git a/doc/蒋欣萍精读部分/Note.java b/doc/蒋欣萍精读部分/Note.java new file mode 100644 index 0000000..a6b4ad4 --- /dev/null +++ b/doc/蒋欣萍精读部分/Note.java @@ -0,0 +1,253 @@ +/* +//单个便签项 + * 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.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; //访问uri相关操作 +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.DataColumns; //数据栏操作处理 +import net.micode.notes.data.Notes.NoteColumns; //笔记栏操作处理 +import net.micode.notes.data.Notes.TextNote; //文本操作处理 + +import java.util.ArrayList; //导入Java,Util,ArrayList + + +public class Note { //定义note类,处理单个便签 + private ContentValues mNoteDiffValues; //声明一个ContentValues变量,用于存储与上次保存时修改的内容 + private NoteData mNoteData; //申明一个NoteData变量,存储note的一些数据 + private static final String TAG = "Note"; //设置软件标签 + /** + * Create a new note id for adding a new note to databases //创建一个新的便签id,用于将新便签加入数据库 + */ + 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); //设定局部修改的初值为1 + values.put(NoteColumns.PARENT_ID, folderId); //将数据写入数据库表格 + Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values); //得到便签ID,并处理一些异常ID + + long noteId = 0; //设置便签ID初值为0 + try { //获取id并且检测是否异常,如果异常则报错 + noteId = Long.valueOf(uri.getPathSegments().get(1)); //获取路径部分赋值为1时的值,转换成long类型赋给noteId + } catch (NumberFormatException e) { // 异常处理提示 + Log.e(TAG, "Get note id error :" + e.toString()); //获取id错误 + noteId = 0; //将便签的ID置为0 + } + if (noteId == -1) { // 错误ID的异常处理 + throw new IllegalStateException("Wrong note id:" + noteId); //非法状态时返回出错便签编号 + } + return noteId; //若没有异常,那么返回这个id + } + + public Note() { //构造Note,实例化note数据 + mNoteDiffValues = new ContentValues(); //存储便签属性 + mNoteData = new NoteData(); //存储便签的内容 + } + + public void setNoteValue(String key, String value) { //设置数据库表格的标签属性数据 + mNoteDiffValues.put(key, value); //设置key值和对应的value值 + mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); //修改之后标志为1 + mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); //设置修改时间为系统当前时间 + } + + public void setTextData(String key, String value) { //设置数据库表格的标签文本内容的数据 + mNoteData.setTextData(key, value); //写入key和value值 + } + + public void setTextDataId(long id) { //设置文本数据的ID + mNoteData.setTextDataId(id); //获取文本id + } + + public long getTextDataId() { //获取文本数据ID + return mNoteData.mTextDataId; //返回文本数据的ID号 + } + + public void setCallDataId(long id) { //设置电话号码数据的ID + mNoteData.setCallDataId(id); + } + + public void setCallData(String key, String value) { //设置数据库表格文件中电话号码数据对应的键值和数据 + mNoteData.setCallData(key, value); //设置key,value值 + + public boolean isLocalModified() { //根据属性特征值判定便签是否被本地修改 + return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified(); //相较上次存在修改size()>0 + } + + public boolean syncNote(Context context, long noteId) { //判断便签是否同步 + if (noteId <= 0) { //便签ID不合法时抛出异常 + throw new IllegalArgumentException("Wrong note id:" + noteId); //抛出异常,弹出对话框错误ID并显示错误ID号 + } + + if (!isLocalModified()) { //如果本地没有发现修改,直接返回1,指示已经同步到数据库中 + 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)) { //如果上下文传输失败,内容与ID为空,则返回false + return false; //失败,返回false + } + + return true; //成功则返回true + } + + private class NoteData { //定义一个基本的便签内容的数据类,主要包含文本数据和电话号码数据 + private long mTextDataId; //文本数据id + + private ContentValues mTextDataValues; //.文本数据内容 + + private long mCallDataId; //电话数据id + + private ContentValues mCallDataValues; //便签的电话号码数据 + + private static final String TAG = "NoteData"; //默认构造函数 + + public NoteData() { // NoteData的构造函数,初始化四个变量 + mTextDataValues = new ContentValues(); //初始化一个文本数据,储存文本 + mCallDataValues = new ContentValues(); //初始化一个电话号码数据,为了储存电话号码 + mTextDataId = 0; //初始化文本数据ID,置为0 + mCallDataId = 0; //初始化电话号码ID,置为0 + } + + boolean isLocalModified() { //判断文本数据是否改变,是返回true,否返回false + return mTextDataValues.size() > 0 || mCallDataValues.size() > 0; + } + + void setTextDataId(long id) { //设置文本数据ID号 + if(id <= 0) { // 对textDataId设置,但是不允许ID小于等于0 + throw new IllegalArgumentException("Text data id should larger than 0"); //ID号默认大于0 + } + mTextDataId = id; //设置电话号码数据ID号 + } + + void setCallDataId(long id) { // 设置呼叫数据ID,若传入参数小于0,则抛出参数错误的异常 + if (id <= 0) { + throw new IllegalArgumentException("Call data id should larger than 0"); //.电话号码数据ID应大于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); //写入参数key,value + mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); //设置属性值为1 + mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); //设置修改时间为当前系统时间 + } + + Uri pushIntoContentResolver(Context context, long noteId) { //使用uri将数据添加到数据库 + /** + * Check for safety + */ + if (noteId <= 0) { //.判断ID是否非法 + throw new IllegalArgumentException("Wrong note id:" + noteId); //如果ID非法抛出异常,显示错误ID + } + + ArrayList operationList = new ArrayList(); + ContentProviderOperation.Builder builder = null; //数据库的操作列表 + + if(mTextDataValues.size() > 0) { //将文本的数据存入DataColums的数据结构中 + mTextDataValues.put(DataColumns.NOTE_ID, noteId); //将文本数据ID传入文本数据库 + if (mTextDataId == 0) { //文本数据ID为零,则此ID是新建默认的ID + mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); //写入拓展类型 + Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, + mTextDataValues); //uri内容接收器插入文本数据库,放在Notes.CONTENT_DATA_URI的路径下 + try { // 尝试设置文本数据ID + setTextDataId(Long.valueOf(uri.getPathSegments().get(1))); + } catch (NumberFormatException e) { + Log.e(TAG, "Insert new text data fail with noteId" + noteId); // 插入数据失败 + mTextDataValues.clear(); //将电话数据加入DataColums的数据结构中,并加入数据库 + return null; + } + } else { + builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( //更新builder对象,追加uri数据和文本ID + Notes.CONTENT_DATA_URI, mTextDataId)); //将uri和id合并后,更新 + builder.withValues(mTextDataValues); //操作列表里添加build操作 + operationList.add(builder.build()); + } + mTextDataValues.clear(); //同步完毕之后,把更改的版本删除 + } + + if(mCallDataValues.size() > 0) { + mCallDataValues.put(DataColumns.NOTE_ID, noteId); // 写入noteID + if (mCallDataId == 0) { //将电话ID设置为uri提供的ID + mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE); + Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, + mCallDataValues); + try { //异常检测,出现异常则清空电话号码的ID + 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 { //当电话号码不为新建时,更新电话号码ID + builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( + Notes.CONTENT_DATA_URI, mCallDataId)); + builder.withValues(mCallDataValues); + operationList.add(builder.build()); //存储过程中的异常处理 + } + mCallDataValues.clear(); + } + + if (operationList.size() > 0) { //存储过程中如果遇到异常,通过如下进行处理 + try { + ContentProviderResult[] results = context.getContentResolver().applyBatch( //Android源码中对通讯录的操作,应用端使用ContentProvider提供的applyBatch,进行批量处理通讯录的联系人入库 + Notes.AUTHORITY, operationList); + return (results == null || results.length == 0 || results[0] == null) ? null //完成后返回一个URI + : 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; + } + } +} diff --git a/doc/蒋欣萍精读部分/NoteItemData.java b/doc/蒋欣萍精读部分/NoteItemData.java new file mode 100644 index 0000000..6f737fd --- /dev/null +++ b/doc/蒋欣萍精读部分/NoteItemData.java @@ -0,0 +1,224 @@ +/* + * 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.ui; //类NoteItemData包含在包net.micode.notes.ui里面 + +import android.content.Context; //引入了一系列的功能包以便实现这个类的相关功能 +import android.database.Cursor; +import android.text.TextUtils; + +import net.micode.notes.data.Contact; +import net.micode.notes.data.Notes; +import net.micode.notes.data.Notes.NoteColumns; //便签栏 +import net.micode.notes.tool.DataUtils; + + +public class NoteItemData { //存储了很多note有关的属性、常用变量 + static final String [] PROJECTION = new String [] { //将便签id,提醒时间,背景颜色id,创建时间,关联桌面挂件,修改时间,便签数量,父文件夹id,文件夹id(便签的一段内容),便签的种类,桌面挂件的id,桌面挂件的类型的名称放在一个PROJECTION的字符串数组里 + NoteColumns.ID, //每个便签的序号ID + NoteColumns.ALERTED_DATE, // 警示的日期设置 + NoteColumns.BG_COLOR_ID, //背景颜色的id + NoteColumns.CREATED_DATE, // 创建的时间 + NoteColumns.HAS_ATTACHMENT, //是否有附件。如果是一个text note,它不会有附件,如果是一个多媒体附件,会有至少一个附件 + NoteColumns.MODIFIED_DATE, //修改日期 + NoteColumns.NOTES_COUNT, //便签数量 + NoteColumns.PARENT_ID, + NoteColumns.SNIPPET, //文件夹名称或者文本注释内容 + NoteColumns.TYPE, + NoteColumns.WIDGET_ID, //小挂件的序号 + NoteColumns.WIDGET_TYPE, //小挂件的类型 + }; + + private static final int ID_COLUMN = 0; //声明类属性,包括背景颜色、手机号码等 + private static final int ALERTED_DATE_COLUMN = 1; + private static final int BG_COLOR_ID_COLUMN = 2; + private static final int CREATED_DATE_COLUMN = 3; + private static final int HAS_ATTACHMENT_COLUMN = 4; + private static final int MODIFIED_DATE_COLUMN = 5; + private static final int NOTES_COUNT_COLUMN = 6; + private static final int PARENT_ID_COLUMN = 7; + private static final int SNIPPET_COLUMN = 8; + private static final int TYPE_COLUMN = 9; + private static final int WIDGET_ID_COLUMN = 10; + private static final int WIDGET_TYPE_COLUMN = 11; + + private long mId; //PROJECT的字符串名称对应的值 + private long mAlertDate; + private int mBgColorId; + private long mCreatedDate; + private boolean mHasAttachment; + private long mModifiedDate; + private int mNotesCount; + private long mParentId; + private String mSnippet; + private int mType; + private int mWidgetId; // 宽度 + private int mWidgetType; //宽度形式 + private String mName; + private String mPhoneNumber; + + private boolean mIsLastItem; //布尔类型,判断是否为最后一项 + private boolean mIsFirstItem; + private boolean mIsOnlyOneItem; //判断是否只有一个便签,或者一个文件夹 + private boolean mIsOneNoteFollowingFolder; //布尔类型 判断便签下是否只有一个文件夹 + private boolean mIsMultiNotesFollowingFolder; + + public NoteItemData(Context context, Cursor cursor) { //初始化NoteItemData利用光标和context获取的内容 + mId = cursor.getLong(ID_COLUMN); //gettype:获取指定数据,并以type类型传回 + mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN); + mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN); + mCreatedDate = cursor.getLong(CREATED_DATE_COLUMN); + mHasAttachment = (cursor.getInt(HAS_ATTACHMENT_COLUMN) > 0) ? true : false; //判断行列 + mModifiedDate = cursor.getLong(MODIFIED_DATE_COLUMN); + mNotesCount = cursor.getInt(NOTES_COUNT_COLUMN); + mParentId = cursor.getLong(PARENT_ID_COLUMN); + mSnippet = cursor.getString(SNIPPET_COLUMN); //获得字符串 + mSnippet = mSnippet.replace(NoteEditActivity.TAG_CHECKED, "").replace( //把每项前的方框符号和✔符号去掉 + NoteEditActivity.TAG_UNCHECKED, ""); + mType = cursor.getInt(TYPE_COLUMN); + mWidgetId = cursor.getInt(WIDGET_ID_COLUMN); + mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN); + + mPhoneNumber = ""; //初始化电话号码的信息 + if (mParentId == Notes.ID_CALL_RECORD_FOLDER) { + mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId); //使用DataUtils类中定义的函数获取电话号码信息 + if (!TextUtils.isEmpty(mPhoneNumber)) { //如果mPhoneNumber变量不等于空string,即这个note的PhoneNumber数据是有效的 + mName = Contact.getContact(context, mPhoneNumber); //mphonenumber里有符合字符串,则用contart功能连接 + if (mName == null) { + mName = mPhoneNumber; //如果没保存名字,就以电话号码命名 + } + } + } + + if (mName == null) { //如果没有对name复制成功,则把name设置为空值 + mName = ""; + } + checkPostion(cursor); //检查光标位置 + } + + private void checkPostion(Cursor cursor) { //根据光标位置设置标记 + mIsLastItem = cursor.isLast() ? true : false; //分别为各种描述状态的变量进行赋值 + mIsFirstItem = cursor.isFirst() ? true : false; + mIsOnlyOneItem = (cursor.getCount() == 1); + mIsMultiNotesFollowingFolder = false; //初始化“多重子文件”“单一子文件”2个标记 + mIsOneNoteFollowingFolder = false; + + if (mType == Notes.TYPE_NOTE && !mIsFirstItem) { //如果是note格式并且不是第一个元素,则获取光标位置并根据上一行信息更改标记 + int position = cursor.getPosition(); //获取光标位置 + if (cursor.moveToPrevious()) { //获取光标位置并看向上一行 + if (cursor.getInt(TYPE_COLUMN) == Notes.TYPE_FOLDER + || cursor.getInt(TYPE_COLUMN) == Notes.TYPE_SYSTEM) { //判断光标是否满足系统或便签格式 + if (cursor.getCount() > (position + 1)) { //若数据行数大于当前位置+1,则为多重文件。否则,单一文件 + mIsMultiNotesFollowingFolder = true; + } else { + mIsOneNoteFollowingFolder = true; + } + } + if (!cursor.moveToNext()) { //若光标不能重新向下走则报错 + throw new IllegalStateException("cursor move to previous but can't move back"); + } + } + } + } + + public boolean isOneFollowingFolder() { // 下面一系列函数都是返回状态值等,用于判断状态 + return mIsOneNoteFollowingFolder; + } + + public boolean isMultiFollowingFolder() { //若数据父id为保存至文件夹模式的id且满足电话号码单元不为空,则isCallRecord为true + return mIsMultiNotesFollowingFolder; + } + + public boolean isLast() { //判断是否是最后一个项 + return mIsLastItem; + } + + public String getCallName() { //获得便签名 + return mName; + } + + public boolean isFirst() { //判断是否是第一个项 + return mIsFirstItem; + } + + public boolean isSingle() { // 判断是否只有一个项 + return mIsOnlyOneItem; + } + + public long getId() { //获得对应的ID值 + return mId; + } + + public long getAlertDate() { //获取设置的提醒时间 + return mAlertDate; + } + + public long getCreatedDate() { //获得创建的时间 + return mCreatedDate; + } + + public boolean hasAttachment() { //判断是否与桌面挂件相关 + return mHasAttachment; + } + + public long getModifiedDate() { //获得修改的时间 + return mModifiedDate; + } + + public int getBgColorId() { //获得背景颜色的索引 + return mBgColorId; + } + + public long getParentId() { //获得父进程的id + return mParentId; + } + + public int getNotesCount() { //获得便签数量 + return mNotesCount; + } + + public long getFolderId () { // 获取文件夹id + return mParentId; + } + + public int getType() { // 获得项的类型 + return mType; + } + + public int getWidgetType() { //获得桌面挂件的类型 + return mWidgetType; + } + + public int getWidgetId() { //获得获得桌面挂件id + return mWidgetId; + } + + public String getSnippet() { //获取便签的外观片段 + return mSnippet; + } + + public boolean hasAlert() { //判读此便签是否有提醒 + return (mAlertDate > 0); + } + + public boolean isCallRecord() { //判断便签项是否为CallRecord + return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber)); //判断是否为电话记录。如果类型匹配,且文本部位空,则表明是电话记录,返回真。否则,返回假 + } + + public static int getNoteType(Cursor cursor) { //获得便签的类型 + return cursor.getInt(TYPE_COLUMN); + } +} diff --git a/doc/蒋欣萍精读部分/ResourceParser.java b/doc/蒋欣萍精读部分/ResourceParser.java new file mode 100644 index 0000000..c871143 --- /dev/null +++ b/doc/蒋欣萍精读部分/ResourceParser.java @@ -0,0 +1,181 @@ +/* + * 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.Context; //引用便签所需要的各个库 +import android.preference.PreferenceManager; // 引入了需要使用的类 + +import net.micode.notes.R; +import net.micode.notes.ui.NotesPreferenceActivity; //导入ui包下的类 + +public class ResourceParser { //界面元素的解析工具类:获取颜色资源,字体资源,图片资源并且在手机中使用 + //对不同颜色的静态常量进行赋值,便于使用 + public static final int YELLOW = 0; //为颜色静态常量赋值 + public static final int BLUE = 1; //定义默认颜色为Yellow + public static final int WHITE = 2; //为字体大小静态常量赋值 + public static final int GREEN = 3; //默认字体大小为Medium + public static final int RED = 4; //对对应的颜色资源索取它们的地址 + + public static final int BG_DEFAULT_COLOR = YELLOW; //默认背景颜色是黄色 + + public static final int TEXT_SMALL = 0; //声明字体大小变量 + public static final int TEXT_MEDIUM = 1; + public static final int TEXT_LARGE = 2; + public static final int TEXT_SUPER = 3; + + public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM; // 默认字体大小是中等大小 + + public static class NoteBgResources { //便签背景资源的获取 + private final static int [] BG_EDIT_RESOURCES = new int [] { + R.drawable.edit_yellow, + R.drawable.edit_blue, + R.drawable.edit_white, + R.drawable.edit_green, + R.drawable.edit_red + }; + + private final static int [] BG_EDIT_TITLE_RESOURCES = new int [] { // 标题颜色的使用情况 + R.drawable.edit_title_yellow, + R.drawable.edit_title_blue, + R.drawable.edit_title_white, + R.drawable.edit_title_green, + R.drawable.edit_title_red + }; + + public static int getNoteBgResource(int id) { //获取便签背景资源id + return BG_EDIT_RESOURCES[id]; + } + + public static int getNoteTitleBgResource(int id) { //获取背景标题资源id + return BG_EDIT_TITLE_RESOURCES[id]; + } + } + + public static int getDefaultBgId(Context context) { //获取默认背景颜色对应id + if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean( //若为随机背景颜色,则返回随机背景颜色 + NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { + return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length); + } else { + return BG_DEFAULT_COLOR; + } + } + + public static class NoteItemBgResources { //便签项目背景资源子类,包括方法:获得首尾项目等背景资源 + private final static int [] BG_FIRST_RESOURCES = new int [] { //对不同drawable的变量(first、normal、last、single)的初始化(声明) + R.drawable.list_yellow_up, + R.drawable.list_blue_up, + R.drawable.list_white_up, + R.drawable.list_green_up, + R.drawable.list_red_up + }; + + private final static int [] BG_NORMAL_RESOURCES = new int [] { //定义了背景的默认资源 + R.drawable.list_yellow_middle, + R.drawable.list_blue_middle, + R.drawable.list_white_middle, + R.drawable.list_green_middle, + R.drawable.list_red_middle + }; + + private final static int [] BG_LAST_RESOURCES = new int [] { //定义背景的下方资源 + R.drawable.list_yellow_down, + R.drawable.list_blue_down, + R.drawable.list_white_down, + R.drawable.list_green_down, + R.drawable.list_red_down, + }; + + private final static int [] BG_SINGLE_RESOURCES = new int [] { + R.drawable.list_yellow_single, + R.drawable.list_blue_single, + R.drawable.list_white_single, + R.drawable.list_green_single, + R.drawable.list_red_single + }; //根据id获取四种类型便签项目背景资源的方法 + + public static int getNoteBgFirstRes(int id) { //获取四种类型便签项目背景资源的方法 + return BG_FIRST_RESOURCES[id]; + } + + public static int getNoteBgLastRes(int id) { // 通过ID寻找last的颜色值 + return BG_LAST_RESOURCES[id]; + } + + public static int getNoteBgSingleRes(int id) { //通过ID获取单个便签背景颜色资源 + return BG_SINGLE_RESOURCES[id]; + } + + public static int getNoteBgNormalRes(int id) { //通过ID寻找normal的颜色值 + return BG_NORMAL_RESOURCES[id]; + } + + public static int getFolderBgRes() { //设置窗口的资源 + return R.drawable.list_folder; + } + } + + public static class WidgetBgResources { // 获取图片资源类的定义 + private final static int [] BG_2X_RESOURCES = new int [] { //2x小窗口背景资源初始化 + R.drawable.widget_2x_yellow, + R.drawable.widget_2x_blue, + R.drawable.widget_2x_white, + R.drawable.widget_2x_green, + R.drawable.widget_2x_red, + }; + + public static int getWidget2xBgResource(int id) { //通过id获得2x的小窗口背景资源 + return BG_2X_RESOURCES[id]; + } + + private final static int [] BG_4X_RESOURCES = new int [] { //4x小窗口背景资源初始化 + R.drawable.widget_4x_yellow, + R.drawable.widget_4x_blue, + R.drawable.widget_4x_white, + R.drawable.widget_4x_green, + R.drawable.widget_4x_red + }; + + public static int getWidget4xBgResource(int id) { //通过id获得4x的小窗口背景资源 + return BG_4X_RESOURCES[id]; + } + } + + public static class TextAppearanceResources { //文本外观资源,包括默认字体,以及获取资源大小 + private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] { //四种不同大小的字体 + R.style.TextAppearanceNormal, + R.style.TextAppearanceMedium, + R.style.TextAppearanceLarge, + R.style.TextAppearanceSuper + }; + + public static int getTexAppearanceResource(int id) { //检测ID是否大于字体大小资源总量,如果是返回默认的结果,如果不是,则返回大小的ID号 + /** + * HACKME: Fix bug of store the resource id in shared preference. //修补bug + * The id may larger than the length of resources, in this case, + * return the {@link ResourceParser#BG_DEFAULT_FONT_SIZE} + */ + if (id >= TEXTAPPEARANCE_RESOURCES.length) { + return BG_DEFAULT_FONT_SIZE; + } + return TEXTAPPEARANCE_RESOURCES[id]; + } + + public static int getResourcesSize() { //返回字体大小资源的长度 + return TEXTAPPEARANCE_RESOURCES.length; + } + } +} diff --git a/doc/蒋欣萍精读部分/WorkingNote.java b/doc/蒋欣萍精读部分/WorkingNote.java new file mode 100644 index 0000000..578dc12 --- /dev/null +++ b/doc/蒋欣萍精读部分/WorkingNote.java @@ -0,0 +1,370 @@ +/* + * 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.model; //设置包 + +import android.appwidget.AppWidgetManager; //该文件调用的其他类 +import android.content.ContentUris; +import android.content.Context; +import android.database.Cursor; //导入其它用来调用的类 +import android.text.TextUtils; +import android.util.Log; + +import net.micode.notes.data.Notes; //小米便签操作的整体属性 +import net.micode.notes.data.Notes.CallNote; +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.DataConstants; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.data.Notes.TextNote; +import net.micode.notes.tool.ResourceParser.NoteBgResources; + + +public class WorkingNote { //设置一个WorkingNote类,包含一些正在进行中的note的属性 + // Note for the working note + private Note mNote; //声明一个Note类型的变量 + // Note Id + private long mNoteId; //声明便签content + // Note content + private String mContent; //声明便签mode + // Note mode + private int mMode; //判断是否为清单模式 + + private long mAlertDate; // 设置闹钟时间 + + private long mModifiedDate; //最后一次修改的时间 + + private int mBgColorId; //背景颜色所对应的ID号 + + private int mWidgetId; //桌面挂件所对应的ID + + private int mWidgetType; //桌面挂件的格式 + + private long mFolderId; //文件夹所对应的ID + + private Context mContext; //当前便签的上下文 + + private static final String TAG = "WorkingNote"; //.声明 一个叫做DATA_PROJECTION字符串数组 + + private boolean mIsDeleted; //是否应该被删除 + + private NoteSettingChangedListener mNoteSettingStatusListener; //判断便签设置值是否改变的监听器 + + public static final String[] DATA_PROJECTION = new String[] { //声明 NOTE_PROJECTION字符串数组 + DataColumns.ID, + DataColumns.CONTENT, + DataColumns.MIME_TYPE, + DataColumns.DATA1, + DataColumns.DATA2, + DataColumns.DATA3, + DataColumns.DATA4, + }; + + public static final String[] NOTE_PROJECTION = new String[] { //保存便签属性信息的string数组 + NoteColumns.PARENT_ID, + NoteColumns.ALERTED_DATE, + NoteColumns.BG_COLOR_ID, + NoteColumns.WIDGET_ID, + NoteColumns.WIDGET_TYPE, + NoteColumns.MODIFIED_DATE + }; //设置数据在哪一列 + + private static final int DATA_ID_COLUMN = 0; //以下定义的整形是上述两个列表中各元素的索引 + + private static final int DATA_CONTENT_COLUMN = 1; + + private static final int DATA_MIME_TYPE_COLUMN = 2; + + private static final int DATA_MODE_COLUMN = 3; + + private static final int NOTE_PARENT_ID_COLUMN = 0; //. 以下6个常量表示便签投影的0-5列 + + private static final int NOTE_ALERTED_DATE_COLUMN = 1; + + private static final int NOTE_BG_COLOR_ID_COLUMN = 2; + + private static final int NOTE_WIDGET_ID_COLUMN = 3; + + private static final int NOTE_WIDGET_TYPE_COLUMN = 4; + + private static final int NOTE_MODIFIED_DATE_COLUMN = 5; + + // New note construct + private WorkingNote(Context context, long folderId) { //.声明一个新的note construct + mContext = context; //构造函数声明 + mAlertDate = 0; //默认提醒日期为0 + mModifiedDate = System.currentTimeMillis(); //获取系统当前时间的方法,返回当前时间 + mFolderId = folderId; //默认最后一次修改日期为当前时间 + mNote = new Note(); //加载一个已存在的便签 + mNoteId = 0; + mIsDeleted = false; + mMode = 0; + mWidgetType = Notes.TYPE_WIDGET_INVALIDE; + } + + // Existing note construct + private WorkingNote(Context context, long noteId, long folderId) { //载入便签 + mContext = context; //通过数据库调用query函数找到第一个条目 + mNoteId = noteId; + mFolderId = folderId; + mIsDeleted = false; + mNote = new Note(); + loadNote(); //加载便签 + } + + private void loadNote() { //输出mNoteId的所有属性值,比如位置信息、颜色信息、控件信息等 + Cursor cursor = mContext.getContentResolver().query( //判断是否存储相应信息 + ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null, + null, null); + + if (cursor != null) { //若存在,则存储相应信息 + if (cursor.moveToFirst()) { //通过判断cursor.moveToFirst()的值为true或false来确定查询结果是否为空 + mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN); //获取文档的属性 + mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN); //关闭cursor游标 + mWidgetId = cursor.getInt(NOTE_WIDGET_ID_COLUMN); + mWidgetType = cursor.getInt(NOTE_WIDGET_TYPE_COLUMN); + mAlertDate = cursor.getLong(NOTE_ALERTED_DATE_COLUMN); + mModifiedDate = cursor.getLong(NOTE_MODIFIED_DATE_COLUMN); + } + cursor.close(); //不存在时返回错误信息 + } else { //否则说明不存在这个便签,加载错误 + Log.e(TAG, "No note with id:" + mNoteId); //未能找到此note,返回异常 + throw new IllegalArgumentException("Unable to find note with id " + mNoteId); //.抛出异常,找不到NoteID + } + loadNoteData(); // 调用方法loadNoteData,导入便签的内容。 + } + + private void loadNoteData() { //加载NoteData + Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION, //调用query函数查找到便签ID为mNoteId的位置,并将光标移动到该位置 + DataColumns.NOTE_ID + "=?", new String[] { + String.valueOf(mNoteId) + }, null); //如果找到的信息不为空,就把内容反馈给用户 + + + if (cursor != null) { //光标存在时,将光标移动到便签的起始位置 + if (cursor.moveToFirst()) { //通过判断cursor.moveToFirst()的值为true或false来确定查询结果是否为空 + do { + String type = cursor.getString(DATA_MIME_TYPE_COLUMN); + if (DataConstants.NOTE.equals(type)) { + mContent = cursor.getString(DATA_CONTENT_COLUMN); + mMode = cursor.getInt(DATA_MODE_COLUMN); + mNote.setTextDataId(cursor.getLong(DATA_ID_COLUMN)); + } else if (DataConstants.CALL_NOTE.equals(type)) { + mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN)); + } else { + Log.d(TAG, "Wrong note type with type:" + type); + } + } while (cursor.moveToNext()); + } + cursor.close(); + } else { //否则说明不存在这个便签,加载错误 + Log.e(TAG, "No data with id:" + mNoteId); //未能找到此note,返回异常 + throw new IllegalArgumentException("Unable to find note's data with id " + mNoteId); //抛出异常,找不到NoteID + } + } + + public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId, //创建一个新便签的构造函数 + int widgetType, int defaultBgColorId) { + WorkingNote note = new WorkingNote(context, folderId); //创建一个新的包含文件ID和内容的便签 + note.setBgColorId(defaultBgColorId); //设置背景颜色 + note.setWidgetId(widgetId); // 设定布局 + note.setWidgetType(widgetType); //设置窗口类型 + return note; + } + + public static WorkingNote load(Context context, long id) { //导入一个新的正在写入的便签 + return new WorkingNote(context, id, 0); //返回便签 + } + + public synchronized boolean saveNote() { //保存便签,成功保存返回true,否则返回false + if (isWorthSaving()) { //判断是否有价值去保存 + if (!existInDatabase()) { //判断是否存在数据库中 + if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) { //没有成功创建便签则记录异常日志 + Log.e(TAG, "Create new note fail with id:" + mNoteId); + return false; + } + } + + mNote.syncNote(mContext, mNoteId); //同步便签内容及便签id + + /** + * Update widget content if there exist any widget of this note + */ + if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID //判断窗口大小是否变化,如果变化也要保存这个变化 + && mWidgetType != Notes.TYPE_WIDGET_INVALIDE + && mNoteSettingStatusListener != null) { + mNoteSettingStatusListener.onWidgetChanged(); + } + return true; + } else { + return false; + } + } + + public boolean existInDatabase() { //判断便签是否已经存在于数据库中,mNoteID >0时存在,返回true,否则返回false + return mNoteId > 0; + } + + private boolean isWorthSaving() { //判断是否有保存的必要性 + if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent)) //已被删除或者不在数据库中但是是空便签或者已经在数据库中但本地没有修改过,都不保存 + || (existInDatabase() && !mNote.isLocalModified())) { + return false; + } else { + return true; //其余情况,要保存,返回true + } + } + + public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) { //设置监听“设置状态改变” + mNoteSettingStatusListener = l; + } + + public void setAlertDate(long date, boolean set) { //设置提醒日期 + if (date != mAlertDate) { //若 mAlertDate与data不同,则更改mAlertDate并设定NoteValue + mAlertDate = date; //为设置的日期打标签 + mNote.setNoteValue(NoteColumns.ALERTED_DATE, String.valueOf(mAlertDate)); + } + if (mNoteSettingStatusListener != null) { //判断是否为空 + mNoteSettingStatusListener.onClockAlertChanged(date, set); + } + } + + public void markDeleted(boolean mark) { //判断是否为空 + mIsDeleted = mark; //设定标记 + if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID + && mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) { + mNoteSettingStatusListener.onWidgetChanged(); //调用mNoteSettingStatusListener的 onWidgetChanged方法 + } + } //调用mNoteSettingStatusListener的 onWidgetChanged方法 + + public void setBgColorId(int id) { //设定背景颜色 + if (id != mBgColorId) { //判断并更新背景颜色 + mBgColorId = id; + if (mNoteSettingStatusListener != null) { + mNoteSettingStatusListener.onBackgroundColorChanged(); + } + mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id)); + } + } + + public void setCheckListMode(int mode) { //设置检查列表模式 + if (mMode != mode) { //.当mMode和mode不相同时进行设定 + if (mNoteSettingStatusListener != null) { + mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode); //设定之后更改mMode + } + mMode = mode; + mNote.setTextData(TextNote.MODE, String.valueOf(mMode)); //储存mode记录的文本数据 + } + } + + public void setWidgetType(int type) { //设置窗口类型 + if (type != mWidgetType) { + mWidgetType = type; //调用Note的setNoteValue方法更改WidgetType + mNote.setNoteValue(NoteColumns.WIDGET_TYPE, String.valueOf(mWidgetType)); + } + } + + public void setWidgetId(int id) { //设置窗口编号 + if (id != mWidgetId) { //判断传入是否与当前id一样,否则更改为传入id + mWidgetId = id; + mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId)); + } //调用Note的setNoteValue方法更改WidgetId + } + + public void setWorkingText(String text) { //设定WorkingText + if (!TextUtils.equals(mContent, text)) { // 判断文本内容是否相同,否则更新文本 + mContent = text; //调用Note的setTextData方法更改WorkingText + mNote.setTextData(DataColumns.CONTENT, mContent); + } + } + + public void convertToCallNote(String phoneNumber, long callDate) { //转换成电话提醒的便签 + mNote.setCallData(CallNote.CALL_DATE, String.valueOf(callDate)); + mNote.setCallData(CallNote.PHONE_NUMBER, phoneNumber); + mNote.setNoteValue(NoteColumns.PARENT_ID, String.valueOf(Notes.ID_CALL_RECORD_FOLDER)); + } + + public boolean hasClockAlert() { //检测是否有时钟提醒,mAlertDate > 0返回真,否则返回假 + return (mAlertDate > 0 ? true : false); + } + + public String getContent() { //获取便签内容 + return mContent; + } + + public long getAlertDate() { //获取待提醒日期 + return mAlertDate; + } + + public long getModifiedDate() { //获取修改之后的日期 + return mModifiedDate; + } + + public int getBgColorResId() { //获取背景颜色id + return NoteBgResources.getNoteBgResource(mBgColorId); + } + + public int getBgColorId() { //获取背景颜色id + return mBgColorId; + } + + public int getTitleBgResId() { //获取标题背景颜色id + return NoteBgResources.getNoteTitleBgResource(mBgColorId); + } + + public int getCheckListMode() { //获取检查列表模式 + return mMode; + } + + public long getNoteId() { //获取便签编号 + return mNoteId; + } + + public long getFolderId() { //获取所在文件夹编号 + return mFolderId; + } + + public int getWidgetId() { //获取窗口编号 + return mWidgetId; + } + + public int getWidgetType() { //创建接口 NoteSettingChangedListener,便签更新监视; +为NoteEditActivity提供接口 + return mWidgetType; + } + + public interface NoteSettingChangedListener { //监听便签设置是否改变 + /** + * Called when the background color of current note has just changed + */ + void onBackgroundColorChanged(); //背景颜色改变按钮 + + /** + * Called when user set clock + */ + void onClockAlertChanged(long date, boolean set); //提醒时间按钮,可进行时间的更改和提醒的开关 + + /** + * Call when user create note from widget + */ + void onWidgetChanged(); //窗口改变 + + /** + * Call when switch between check list mode and normal mode + * @param oldMode is previous mode before change + * @param newMode is new mode + */ + void onCheckListModeChanged(int oldMode, int newMode); //列表检查模式改变 + } +} From b256b747f1f5272a183ae3acc4691a134b43b89c Mon Sep 17 00:00:00 2001 From: wlj <1449591383@qq.com> Date: Sun, 16 Apr 2023 19:13:52 +0800 Subject: [PATCH 6/7] a --- doc/新建文本文档.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 doc/新建文本文档.txt diff --git a/doc/新建文本文档.txt b/doc/新建文本文档.txt new file mode 100644 index 0000000..e69de29 From bc63158d2b234db5d117062d9fdd29ddc57740be Mon Sep 17 00:00:00 2001 From: wlj <1449591383@qq.com> Date: Sun, 16 Apr 2023 19:32:40 +0800 Subject: [PATCH 7/7] a --- doc/新建文本文档.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 doc/新建文本文档.txt diff --git a/doc/新建文本文档.txt b/doc/新建文本文档.txt deleted file mode 100644 index e69de29..0000000