1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.android.inputmethod.dictionarypack; 18 19 import android.content.ContentValues; 20 import android.content.Context; 21 import android.database.sqlite.SQLiteDatabase; 22 import android.database.sqlite.SQLiteOpenHelper; 23 24 import java.text.SimpleDateFormat; 25 import java.util.Date; 26 import java.util.Locale; 27 28 /** 29 * Class to keep long-term log. This is inactive in production, and is only for debug purposes. 30 */ 31 public class PrivateLog { 32 33 public static final boolean DEBUG = DictionaryProvider.DEBUG; 34 35 private static final String LOG_DATABASE_NAME = "log"; 36 private static final String LOG_TABLE_NAME = "log"; 37 private static final int LOG_DATABASE_VERSION = 1; 38 39 private static final String COLUMN_DATE = "date"; 40 private static final String COLUMN_EVENT = "event"; 41 42 private static final String LOG_TABLE_CREATE = "CREATE TABLE " + LOG_TABLE_NAME + " (" 43 + COLUMN_DATE + " TEXT," 44 + COLUMN_EVENT + " TEXT);"; 45 46 static final SimpleDateFormat sDateFormat = new SimpleDateFormat( 47 "yyyy/MM/dd HH:mm:ss", Locale.ROOT); 48 49 private static PrivateLog sInstance = new PrivateLog(); 50 private static DebugHelper sDebugHelper = null; 51 PrivateLog()52 private PrivateLog() { 53 } 54 getInstance(final Context context)55 public static synchronized PrivateLog getInstance(final Context context) { 56 if (!DEBUG) return sInstance; 57 synchronized(PrivateLog.class) { 58 if (sDebugHelper == null) { 59 sDebugHelper = new DebugHelper(context); 60 } 61 return sInstance; 62 } 63 } 64 65 static class DebugHelper extends SQLiteOpenHelper { 66 DebugHelper(final Context context)67 DebugHelper(final Context context) { 68 super(context, LOG_DATABASE_NAME, null, LOG_DATABASE_VERSION); 69 } 70 71 @Override onCreate(SQLiteDatabase db)72 public void onCreate(SQLiteDatabase db) { 73 if (!DEBUG) return; 74 db.execSQL(LOG_TABLE_CREATE); 75 insert(db, "Created table"); 76 } 77 78 @Override onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)79 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 80 if (!DEBUG) return; 81 // Remove all data. 82 db.execSQL("DROP TABLE IF EXISTS " + LOG_TABLE_NAME); 83 onCreate(db); 84 insert(db, "Upgrade finished"); 85 } 86 insert(SQLiteDatabase db, String event)87 static void insert(SQLiteDatabase db, String event) { 88 if (!DEBUG) return; 89 final ContentValues c = new ContentValues(2); 90 c.put(COLUMN_DATE, sDateFormat.format(new Date(System.currentTimeMillis()))); 91 c.put(COLUMN_EVENT, event); 92 db.insert(LOG_TABLE_NAME, null, c); 93 } 94 95 } 96 log(String event)97 public static void log(String event) { 98 if (!DEBUG) return; 99 final SQLiteDatabase l = sDebugHelper.getWritableDatabase(); 100 DebugHelper.insert(l, event); 101 } 102 } 103