• 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;
18 
19 import android.app.PendingIntent;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.os.Parcelable;
24 
25 import com.googlecode.android_scripting.interpreter.Interpreter;
26 
27 import java.io.File;
28 
29 public class IntentBuilders {
30   /** An arbitrary value that is used to identify pending intents for executing scripts. */
31   private static final int EXECUTE_SCRIPT_REQUEST_CODE = 0x12f412a;
32 
IntentBuilders()33   private IntentBuilders() {
34     // Utility class.
35   }
36 
buildTriggerServiceIntent()37   public static Intent buildTriggerServiceIntent() {
38     Intent intent = new Intent();
39     intent.setComponent(Constants.TRIGGER_SERVICE_COMPONENT_NAME);
40     return intent;
41   }
42 
43   /**
44    * Builds an intent that will launch a script in the background.
45    *
46    * @param script
47    *          the script to launch
48    * @return the intent that will launch the script
49    */
buildStartInBackgroundIntent(File script)50   public static Intent buildStartInBackgroundIntent(File script) {
51     final ComponentName componentName = Constants.SL4A_SERVICE_LAUNCHER_COMPONENT_NAME;
52     Intent intent = new Intent();
53     intent.setComponent(componentName);
54     intent.setAction(Constants.ACTION_LAUNCH_BACKGROUND_SCRIPT);
55     intent.putExtra(Constants.EXTRA_SCRIPT_PATH, script.getAbsolutePath());
56     return intent;
57   }
58 
59   /**
60    * Builds an intent that launches a script in a terminal.
61    *
62    * @param script
63    *          the script to launch
64    * @return the intent that will launch the script
65    */
buildStartInTerminalIntent(File script)66   public static Intent buildStartInTerminalIntent(File script) {
67     final ComponentName componentName = Constants.SL4A_SERVICE_LAUNCHER_COMPONENT_NAME;
68     Intent intent = new Intent();
69     intent.setComponent(componentName);
70     intent.setAction(Constants.ACTION_LAUNCH_FOREGROUND_SCRIPT);
71     intent.putExtra(Constants.EXTRA_SCRIPT_PATH, script.getAbsolutePath());
72     return intent;
73   }
74 
75   /**
76    * Builds an intent that launches an interpreter.
77    *
78    * @param interpreterName
79    *          the interpreter to launch
80    * @return the intent that will launch the interpreter
81    */
buildStartInterpreterIntent(String interpreterName)82   public static Intent buildStartInterpreterIntent(String interpreterName) {
83     final ComponentName componentName = Constants.SL4A_SERVICE_LAUNCHER_COMPONENT_NAME;
84     Intent intent = new Intent();
85     intent.setComponent(componentName);
86     intent.setAction(Constants.ACTION_LAUNCH_INTERPRETER);
87     intent.putExtra(Constants.EXTRA_INTERPRETER_NAME, interpreterName);
88     return intent;
89   }
90 
91   /**
92    * Builds an intent that creates a shortcut to launch the provided interpreter.
93    *
94    * @param interpreter
95    *          the interpreter to link to
96    * @param iconResource
97    *          the icon resource to associate with the shortcut
98    * @return the intent that will create the shortcut
99    */
buildInterpreterShortcutIntent(Interpreter interpreter, Parcelable iconResource)100   public static Intent buildInterpreterShortcutIntent(Interpreter interpreter,
101       Parcelable iconResource) {
102     Intent intent = new Intent();
103     intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
104         buildStartInterpreterIntent(interpreter.getName()));
105     intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, interpreter.getNiceName());
106     intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
107     return intent;
108   }
109 
110   /**
111    * Builds an intent that creates a shortcut to launch the provided script in the background.
112    *
113    * @param script
114    *          the script to link to
115    * @param iconResource
116    *          the icon resource to associate with the shortcut
117    * @return the intent that will create the shortcut
118    */
buildBackgroundShortcutIntent(File script, Parcelable iconResource)119   public static Intent buildBackgroundShortcutIntent(File script, Parcelable iconResource) {
120     Intent intent = new Intent();
121     intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, buildStartInBackgroundIntent(script));
122     intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, script.getName());
123     intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
124     return intent;
125   }
126 
127   /**
128    * Builds an intent that creates a shortcut to launch the provided script in a terminal.
129    *
130    * @param script
131    *          the script to link to
132    * @param iconResource
133    *          the icon resource to associate with the shortcut
134    * @return the intent that will create the shortcut
135    */
buildTerminalShortcutIntent(File script, Parcelable iconResource)136   public static Intent buildTerminalShortcutIntent(File script, Parcelable iconResource) {
137     Intent intent = new Intent();
138     intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, buildStartInTerminalIntent(script));
139     intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, script.getName());
140     intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
141     return intent;
142   }
143 
144   /**
145    * Creates a pending intent that can be used to start the trigger service.
146    *
147    * @param context
148    *          the context under whose authority to launch the intent
149    *
150    * @return {@link PendingIntent} object for running the trigger service
151    */
buildTriggerServicePendingIntent(Context context)152   public static PendingIntent buildTriggerServicePendingIntent(Context context) {
153     final Intent intent = buildTriggerServiceIntent();
154     return PendingIntent.getService(context, EXECUTE_SCRIPT_REQUEST_CODE, intent,
155         PendingIntent.FLAG_UPDATE_CURRENT);
156   }
157 }
158