blob: be38d7045185d88e7f2ec5ac598ae800e0693bda [file] [log] [blame]
John Reck0ebd3ac2010-12-09 11:14:04 -08001/*
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
18package com.android.browser;
19
20import android.content.ContentResolver;
21import android.content.ContentUris;
22import android.content.ContentValues;
23import android.content.Context;
24import android.database.Cursor;
25import android.os.Handler;
26import android.os.HandlerThread;
27import android.os.Looper;
28import android.os.Message;
29import android.provider.BrowserContract.History;
30
31public 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}