• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 package com.android.contacts.tests;
18 
19 import android.app.IntentService;
20 import android.content.Intent;
21 import android.database.Cursor;
22 import android.net.Uri;
23 import android.text.TextUtils;
24 import android.util.Log;
25 
26 /**
27  * A service that executes a query specified by an intent and dump the result on logcat.  Use the
28  * "am" command to launch it.
29  *
30    Usage:
31      adb shell am startservice -d URI \
32        [-e p OPTIONAL PROJECTION] [-e s OPTIONAL SELECTION] [-e s OPTIONAL ORDER BY]  \
33        com.android.contacts.tests/.QueryService
34 
35    Example:
36 
37    adb shell am startservice -d content://com.android.contacts/directories \
38      -e p accountName,accountType -e s 'accountName NOT NULL' -e o '_id'  \
39      com.android.contacts.tests/.QueryService
40  */
41 public class QueryService extends IntentService {
42     private static final String TAG = "contactsquery";
43 
44     private static final String EXTRA_PROJECTION = "p";
45     private static final String EXTRA_SELECTION = "s";
46     private static final String EXTRA_ORDER = "o";
47     private static final String NULL_STRING = "*null*";
48     private static final String SEPARATOR = "|";
49 
QueryService()50     public QueryService() {
51         super("ContactsQueryService");
52     }
53 
54     @Override
onHandleIntent(Intent intent)55     protected void onHandleIntent(Intent intent) {
56         final Uri uri = intent.getData();
57         // Unfortunately "am" doesn't support string arrays...
58         final String projection = intent.getStringExtra(EXTRA_PROJECTION);
59         final String selection = intent.getStringExtra(EXTRA_SELECTION);
60         final String order = intent.getStringExtra(EXTRA_ORDER);
61 
62         Log.i(TAG, "URI: " + uri);
63         Log.i(TAG, "Projection: " + projection);
64         Log.i(TAG, "Selection: " + selection);
65 
66         try {
67             Cursor c = getContentResolver().query(uri, parseProjection(projection), selection, null,
68                     order);
69             if (c == null) {
70                 Log.i(TAG, "(no results)");
71                 return;
72             }
73             StringBuilder sb = new StringBuilder();
74             try {
75                 Log.i(TAG, "Result count: " + c.getCount());
76 
77                 final int columnCount = c.getColumnCount();
78 
79                 sb.setLength(0);
80                 for (int i = 0; i < columnCount; i++) {
81                     add(sb, c.getColumnName(i));
82                 }
83                 Log.i(TAG, sb.toString());
84 
85                 c.moveToPosition(-1);
86                 while (c.moveToNext()) {
87                     sb.setLength(0);
88                     for (int i = 0; i < columnCount; i++) {
89                         add(sb, c.getString(i));
90                     }
91                     Log.i(TAG, sb.toString());
92                 }
93             } finally {
94                 c.close();
95             }
96         } catch (Exception e) {
97             Log.e(TAG, "Exeption while executing query", e);
98         }
99     }
100 
add(StringBuilder sb, String s)101     private StringBuilder add(StringBuilder sb, String s) {
102         if (sb.length() > 0) {
103             sb.append(SEPARATOR);
104         }
105         sb.append(s == null ? NULL_STRING : s);
106         return sb;
107     }
108 
parseProjection(String projectionString)109     private static String[] parseProjection(String projectionString) {
110         if (TextUtils.isEmpty(projectionString)) {
111             return null; // all columns
112         }
113         final String[] columns = projectionString.split(",");
114         if (columns.length == 0) {
115             return null; // all columns
116         }
117         return columns;
118     }
119 }
120