• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.googlecode.android_scripting.provider;
18 
19 import android.app.SearchManager;
20 import android.content.ContentProvider;
21 import android.content.ContentValues;
22 import android.content.UriMatcher;
23 import android.database.Cursor;
24 import android.database.MatrixCursor;
25 import android.net.Uri;
26 import android.provider.BaseColumns;
27 
28 import com.google.common.base.Predicate;
29 import com.google.common.collect.Collections2;
30 import com.googlecode.android_scripting.facade.FacadeConfiguration;
31 import com.googlecode.android_scripting.rpc.MethodDescriptor;
32 import com.googlecode.android_scripting.rpc.Rpc;
33 import com.googlecode.android_scripting.rpc.RpcDeprecated;
34 import com.googlecode.android_scripting.rpc.RpcMinSdk;
35 
36 import java.lang.reflect.Method;
37 import java.util.Collection;
38 
39 public class ApiProvider extends ContentProvider {
40 
41   public static final String SINGLE_MIME = "vnd.android.cursor.item/vnd.sl4a.api";
42 
43   private static final int SUGGESTIONS_ID = 1;
44 
45   public static final String AUTHORITY = ApiProvider.class.getName().toLowerCase();
46   public static final String SUGGESTIONS = "searchSuggestions/*/*";
47 
48   private final UriMatcher mUriMatcher;
49   private final Collection<MethodDescriptor> mMethodDescriptors;
50 
ApiProvider()51   public ApiProvider() {
52     mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
53     mUriMatcher.addURI(AUTHORITY, SUGGESTIONS, SUGGESTIONS_ID);
54     mMethodDescriptors =
55         Collections2.filter(FacadeConfiguration.collectMethodDescriptors(),
56             new Predicate<MethodDescriptor>() {
57               @Override
58               public boolean apply(MethodDescriptor descriptor) {
59                 Method method = descriptor.getMethod();
60                 if (method.isAnnotationPresent(RpcDeprecated.class)) {
61                   return false;
62                 } else if (method.isAnnotationPresent(RpcMinSdk.class)) {
63                   int requiredSdkLevel = method.getAnnotation(RpcMinSdk.class).value();
64                   if (FacadeConfiguration.getSdkLevel() < requiredSdkLevel) {
65                     return false;
66                   }
67                 }
68                 return true;
69               }
70             });
71   }
72 
73   @Override
delete(Uri uri, String selection, String[] selectionArgs)74   public int delete(Uri uri, String selection, String[] selectionArgs) {
75     return 0;
76   }
77 
78   @Override
getType(Uri uri)79   public String getType(Uri uri) {
80     return SINGLE_MIME;
81   }
82 
83   @Override
insert(Uri uri, ContentValues values)84   public Uri insert(Uri uri, ContentValues values) {
85     return null;
86   }
87 
88   @Override
onCreate()89   public boolean onCreate() {
90     return true;
91   }
92 
93   @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)94   public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
95       String sortOrder) {
96     switch (mUriMatcher.match(uri)) {
97     case SUGGESTIONS_ID:
98       String query = uri.getLastPathSegment().toLowerCase();
99       return querySearchSuggestions(query);
100     }
101     return null;
102   }
103 
querySearchSuggestions(String query)104   private Cursor querySearchSuggestions(String query) {
105     String[] columns =
106         { BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1,
107           SearchManager.SUGGEST_COLUMN_TEXT_2, SearchManager.SUGGEST_COLUMN_QUERY };
108     MatrixCursor cursor = new MatrixCursor(columns);
109     int index = 0;
110     for (MethodDescriptor descriptor : mMethodDescriptors) {
111       String name = descriptor.getMethod().getName();
112       if (!name.toLowerCase().contains(query)) {
113         continue;
114       }
115       String description = descriptor.getMethod().getAnnotation(Rpc.class).description();
116       description = description.replaceAll("\\s+", " ");
117       Object[] row = { index, name, description, name };
118       cursor.addRow(row);
119       ++index;
120     }
121     return cursor;
122   }
123 
124   @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)125   public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
126     return 0;
127   }
128 }
129