ApiProvider.java revision 9f32db87b486c93a0ea71eb1781ee45676b8bf8b
1/*
2 * Copyright (C) 2016 Google Inc.
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
17package com.googlecode.android_scripting.provider;
18
19import android.app.SearchManager;
20import android.content.ContentProvider;
21import android.content.ContentValues;
22import android.content.UriMatcher;
23import android.database.Cursor;
24import android.database.MatrixCursor;
25import android.net.Uri;
26import android.provider.BaseColumns;
27
28import com.google.common.base.Predicate;
29import com.google.common.collect.Collections2;
30import com.googlecode.android_scripting.facade.FacadeConfiguration;
31import com.googlecode.android_scripting.rpc.MethodDescriptor;
32import com.googlecode.android_scripting.rpc.Rpc;
33import com.googlecode.android_scripting.rpc.RpcDeprecated;
34import com.googlecode.android_scripting.rpc.RpcMinSdk;
35
36import java.lang.reflect.Method;
37import java.util.Collection;
38
39public 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
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
74  public int delete(Uri uri, String selection, String[] selectionArgs) {
75    return 0;
76  }
77
78  @Override
79  public String getType(Uri uri) {
80    return SINGLE_MIME;
81  }
82
83  @Override
84  public Uri insert(Uri uri, ContentValues values) {
85    return null;
86  }
87
88  @Override
89  public boolean onCreate() {
90    return true;
91  }
92
93  @Override
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
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
125  public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
126    return 0;
127  }
128}
129