John Reck | 0ebd3ac | 2010-12-09 11:14:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | package com.android.browser; |
| 19 | |
| 20 | import android.content.ContentResolver; |
| 21 | import android.content.ContentUris; |
| 22 | import android.content.ContentValues; |
| 23 | import android.content.Context; |
| 24 | import android.database.Cursor; |
| 25 | import android.os.Handler; |
| 26 | import android.os.HandlerThread; |
| 27 | import android.os.Looper; |
| 28 | import android.os.Message; |
| 29 | import android.provider.BrowserContract.History; |
| 30 | |
| 31 | public class DataController { |
| 32 | // Message IDs |
| 33 | private static final int HISTORY_UPDATE_VISITED = 100; |
| 34 | private static final int HISTORY_UPDATE_TITLE = 101; |
| 35 | private static DataController sInstance; |
| 36 | |
| 37 | private Context mContext; |
| 38 | private Handler mHandler; |
| 39 | |
| 40 | /* package */ static DataController getInstance(Context c) { |
| 41 | if (sInstance == null) { |
| 42 | sInstance = new DataController(c); |
| 43 | } |
| 44 | return sInstance; |
| 45 | } |
| 46 | |
| 47 | private DataController(Context c) { |
| 48 | mContext = c.getApplicationContext(); |
| 49 | HandlerThread thread = new HandlerThread("DataController"); |
| 50 | thread.setDaemon(true); |
| 51 | thread.start(); |
| 52 | mHandler = new DataControllerHandler(thread.getLooper()); |
| 53 | } |
| 54 | |
| 55 | public void updateVisitedHistory(String url) { |
| 56 | mHandler.obtainMessage(HISTORY_UPDATE_VISITED, url).sendToTarget(); |
| 57 | } |
| 58 | |
| 59 | public void updateHistoryTitle(String url, String title) { |
| 60 | mHandler.obtainMessage(HISTORY_UPDATE_TITLE, new String[] { url, title }) |
| 61 | .sendToTarget(); |
| 62 | } |
| 63 | |
| 64 | class DataControllerHandler extends Handler { |
| 65 | public DataControllerHandler(Looper looper) { |
| 66 | super(looper); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void handleMessage(Message msg) { |
| 71 | switch (msg.what) { |
| 72 | case HISTORY_UPDATE_VISITED: |
| 73 | doUpdateVisitedHistory((String) msg.obj); |
| 74 | break; |
| 75 | case HISTORY_UPDATE_TITLE: |
| 76 | String[] args = (String[]) msg.obj; |
| 77 | doUpdateHistoryTitle(args[0], args[1]); |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private void doUpdateVisitedHistory(String url) { |
| 84 | ContentResolver cr = mContext.getContentResolver(); |
| 85 | Cursor c = null; |
| 86 | try { |
| 87 | c = cr.query(History.CONTENT_URI, new String[] { History._ID, History.VISITS }, |
| 88 | History.URL + "=?", new String[] { url }, null); |
| 89 | if (c.moveToFirst()) { |
| 90 | ContentValues values = new ContentValues(); |
| 91 | values.put(History.VISITS, c.getInt(1) + 1); |
| 92 | values.put(History.DATE_LAST_VISITED, System.currentTimeMillis()); |
| 93 | cr.update(ContentUris.withAppendedId(History.CONTENT_URI, c.getLong(0)), |
| 94 | values, null, null); |
| 95 | } else { |
| 96 | android.provider.Browser.truncateHistory(cr); |
| 97 | ContentValues values = new ContentValues(); |
| 98 | values.put(History.URL, url); |
| 99 | values.put(History.VISITS, 1); |
| 100 | values.put(History.DATE_LAST_VISITED, System.currentTimeMillis()); |
| 101 | values.put(History.TITLE, url); |
| 102 | values.put(History.DATE_CREATED, 0); |
| 103 | values.put(History.USER_ENTERED, 0); |
| 104 | cr.insert(History.CONTENT_URI, values); |
| 105 | } |
| 106 | } finally { |
| 107 | if (c != null) c.close(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private void doUpdateHistoryTitle(String url, String title) { |
| 112 | ContentResolver cr = mContext.getContentResolver(); |
| 113 | ContentValues values = new ContentValues(); |
| 114 | values.put(History.TITLE, title); |
| 115 | cr.update(History.CONTENT_URI, values, History.URL + "=?", |
| 116 | new String[] { url }); |
| 117 | } |
| 118 | } |