Clover Coverage Report - Main Coverage Report
Coverage timestamp: ven dic 19 2014 16:47:52 EST
../../../../img/srcFileCovDistChart0.png 84% of files have more coverage
41   135   15   3,73
8   104   0,37   5,5
11     1,36  
2    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  BicyclesSearchDatabase       Line # 18 24 8 0% 0.0
  BicyclesSearchDatabase.BicycleSearchOpenHelper       Line # 89 17 7 0% 0.0
 
No Tests
 
1    package com.dreamcatcher.bicycle.dataset;
2   
3    import java.util.ArrayList;
4    import java.util.HashMap;
5   
6    import android.app.SearchManager;
7    import android.content.ContentValues;
8    import android.content.Context;
9    import android.database.Cursor;
10    import android.database.sqlite.SQLiteDatabase;
11    import android.database.sqlite.SQLiteOpenHelper;
12    import android.database.sqlite.SQLiteQueryBuilder;
13    import android.provider.BaseColumns;
14   
15    import com.dreamcatcher.bicycle.BicycleApp;
16    import com.dreamcatcher.bicycle.vo.BicycleStationInfo;
17   
 
18    public class BicyclesSearchDatabase {
19    private static final String DATABASE_NAME = "bicyclestation";
20    private static final String FTS_VIRTUAL_TABLE = "FTSbicyclestation";
21    private static final int DATABASE_VERSION = 2;
22   
23    private static final String BICYCLE_ID = "bicycle_id";
24    private static final String BICYCLE_NAME_PINYIN = "bicycle_name";
25    private static final HashMap<String,String> mColumnMap = buildColumnMap();
26    private final BicycleSearchOpenHelper mBicycleSearchOpenHelper;
27   
28    private static BicyclesSearchDatabase mInstance = null;
29   
 
30  0 toggle public static BicyclesSearchDatabase getInstance(){
31  0 if(mInstance == null){
32  0 mInstance = new BicyclesSearchDatabase();
33    }
34  0 return mInstance;
35    }
36   
 
37  0 toggle private BicyclesSearchDatabase(){
38  0 mBicycleSearchOpenHelper = new BicycleSearchOpenHelper(BicycleApp.getInstance());
39    }
40   
 
41  0 toggle private static HashMap<String,String> buildColumnMap() {
42  0 HashMap<String,String> map = new HashMap<String,String>();
43  0 map.put(BICYCLE_ID, BICYCLE_ID);
44  0 map.put(BICYCLE_NAME_PINYIN, BICYCLE_NAME_PINYIN);
45  0 map.put(BaseColumns._ID, "rowid AS " +
46    BaseColumns._ID);
47  0 map.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID, "rowid AS " +
48    SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID);
49  0 map.put(SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, "rowid AS " +
50    SearchManager.SUGGEST_COLUMN_SHORTCUT_ID);
51  0 return map;
52    }
53   
 
54  0 toggle public Cursor queryBicycleStationName(String query, String[] columns){
55  0 String selection = BICYCLE_NAME_PINYIN + " MATCH ?";
56  0 String[] selectionArgs = new String[] {query+"*"};
57  0 return query(selection, selectionArgs, columns);
58    }
59   
60   
61    /**
62    * Performs a database query.
63    * @param selection The selection clause
64    * @param selectionArgs Selection arguments for "?" components in the selection
65    * @param columns The columns to return
66    * @return A Cursor over all rows matching the query
67    */
 
68  0 toggle private Cursor query(String selection, String[] selectionArgs, String[] columns) {
69    /* The SQLiteBuilder provides a map for all possible columns requested to
70    * actual columns in the database, creating a simple column alias mechanism
71    * by which the ContentProvider does not need to know the real column names
72    */
73  0 SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
74  0 builder.setTables(FTS_VIRTUAL_TABLE);
75  0 builder.setProjectionMap(mColumnMap);
76   
77  0 Cursor cursor = builder.query(mBicycleSearchOpenHelper.getReadableDatabase(),
78    columns, selection, selectionArgs, null, null, null);
79   
80  0 if (cursor == null) {
81  0 return null;
82  0 } else if (!cursor.moveToFirst()) {
83  0 cursor.close();
84  0 return null;
85    }
86  0 return cursor;
87    }
88   
 
89    private static class BicycleSearchOpenHelper extends SQLiteOpenHelper{
90    private SQLiteDatabase mDatabase;
91    private static final String FTS_TABLE_CREATE =
92    "CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE +
93    " USING fts3 (" +
94    BICYCLE_ID + ", " +
95    BICYCLE_NAME_PINYIN + ");";
96   
 
97  0 toggle public BicycleSearchOpenHelper(Context context){
98  0 super(context, DATABASE_NAME, null, DATABASE_VERSION);
99    }
100   
 
101  0 toggle @Override
102    public void onCreate(SQLiteDatabase db) {
103  0 mDatabase = db;
104  0 db.execSQL(FTS_TABLE_CREATE);
105  0 addBicycleData();
106    }
107   
 
108  0 toggle private void addBicycleData(){
109  0 new Thread(new Runnable() {
 
110  0 toggle public void run() {
111  0 ArrayList<BicycleStationInfo> arrayList = BicycleDataset.getInstance().getBicycleStationInfos();
112  0 for(int i = 0, n = arrayList.size(); i < n; i++){
113  0 BicycleStationInfo bicycleStationInfo = arrayList.get(i);
114  0 int id = bicycleStationInfo.getId();
115  0 String name = bicycleStationInfo.getName();
116  0 addBicycleName(id, name);
117    }
118    }
119    }).start();
120    }
121   
 
122  0 toggle private long addBicycleName(int id, String name){
123  0 ContentValues contentValues = new ContentValues();
124  0 contentValues.put(BICYCLE_ID, id);
125  0 contentValues.put(BICYCLE_NAME_PINYIN, name);
126  0 return mDatabase.insert(FTS_VIRTUAL_TABLE, null, contentValues);
127    }
128   
 
129  0 toggle @Override
130    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
131  0 db.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE);
132  0 onCreate(db);
133    }
134    }
135    }