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.facade; 18 19 import android.app.SearchManager; 20 import android.content.ActivityNotFoundException; 21 import android.content.Intent; 22 import android.net.Uri; 23 import android.provider.Contacts.People; 24 25 import com.googlecode.android_scripting.Log; 26 import com.googlecode.android_scripting.jsonrpc.RpcReceiver; 27 import com.googlecode.android_scripting.rpc.Rpc; 28 import com.googlecode.android_scripting.rpc.RpcOptional; 29 import com.googlecode.android_scripting.rpc.RpcParameter; 30 31 import java.io.File; 32 33 import org.json.JSONException; 34 import org.json.JSONObject; 35 36 /** 37 * A selection of commonly used intents. <br> 38 * <br> 39 * These can be used to trigger some common tasks. 40 * 41 */ 42 public class CommonIntentsFacade extends RpcReceiver { 43 44 private final AndroidFacade mAndroidFacade; 45 CommonIntentsFacade(FacadeManager manager)46 public CommonIntentsFacade(FacadeManager manager) { 47 super(manager); 48 mAndroidFacade = manager.getReceiver(AndroidFacade.class); 49 } 50 51 @Override shutdown()52 public void shutdown() { 53 } 54 55 @Rpc(description = "Display content to be picked by URI (e.g. contacts)", returns = "A map of result values.") pick(@pcParametername = "uri") String uri)56 public Intent pick(@RpcParameter(name = "uri") String uri) throws JSONException { 57 return mAndroidFacade.startActivityForResult(Intent.ACTION_PICK, uri, null, null, null, null); 58 } 59 60 @Rpc(description = "Starts the barcode scanner.", returns = "A Map representation of the result Intent.") scanBarcode()61 public Intent scanBarcode() throws JSONException { 62 try { 63 return mAndroidFacade.startActivityForResult("com.google.zxing.client.android.SCAN", null, 64 null, null, null, null); 65 } catch (ActivityNotFoundException e) { 66 Log.e("No Activity found to scan a barcode!", e); 67 return null; 68 } 69 } 70 view(Uri uri, String type)71 private void view(Uri uri, String type) { 72 Intent intent = new Intent(Intent.ACTION_VIEW); 73 intent.setDataAndType(uri, type); 74 mAndroidFacade.startActivity(intent); 75 } 76 77 @Rpc(description = "Start activity with view action by URI (i.e. browser, contacts, etc.).") view( @pcParametername = "uri") String uri, @RpcParameter(name = "type", description = "MIME type/subtype of the URI") @RpcOptional String type, @RpcParameter(name = "extras", description = "a Map of extras to add to the Intent") @RpcOptional JSONObject extras)78 public void view( 79 @RpcParameter(name = "uri") String uri, 80 @RpcParameter(name = "type", description = "MIME type/subtype of the URI") @RpcOptional String type, 81 @RpcParameter(name = "extras", description = "a Map of extras to add to the Intent") @RpcOptional JSONObject extras) 82 throws Exception { 83 mAndroidFacade.startActivity(Intent.ACTION_VIEW, uri, type, extras, true, null, null); 84 } 85 86 @Rpc(description = "Opens a map search for query (e.g. pizza, 123 My Street).") viewMap(@pcParametername = "query, e.g. pizza, 123 My Street") String query)87 public void viewMap(@RpcParameter(name = "query, e.g. pizza, 123 My Street") String query) 88 throws Exception { 89 view("geo:0,0?q=" + query, null, null); 90 } 91 92 @Rpc(description = "Opens the list of contacts.") viewContacts()93 public void viewContacts() throws JSONException { 94 view(People.CONTENT_URI, null); 95 } 96 97 @Rpc(description = "Opens the browser to display a local HTML file.") viewHtml( @pcParametername = "path", description = "the path to the HTML file") String path)98 public void viewHtml( 99 @RpcParameter(name = "path", description = "the path to the HTML file") String path) 100 throws JSONException { 101 File file = new File(path); 102 view(Uri.fromFile(file), "text/html"); 103 } 104 105 @Rpc(description = "Starts a search for the given query.") search(@pcParametername = "query") String query)106 public void search(@RpcParameter(name = "query") String query) { 107 Intent intent = new Intent(Intent.ACTION_SEARCH); 108 intent.putExtra(SearchManager.QUERY, query); 109 mAndroidFacade.startActivity(intent); 110 } 111 } 112