Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/Plugin.java
Show First 20 Lines • Show All 69 Lines • ▼ Show 20 Line(s) | 43 | public abstract class Plugin { | |||
---|---|---|---|---|---|
70 | 70 | | |||
71 | /** | 71 | /** | ||
72 | * Return the internal plugin name, that will be used as a | 72 | * Return the internal plugin name, that will be used as a | ||
73 | * unique key to distinguish it. Use the class name as key. | 73 | * unique key to distinguish it. Use the class name as key. | ||
74 | */ | 74 | */ | ||
75 | public String getPluginKey() { | 75 | public String getPluginKey() { | ||
76 | return getPluginKey(this.getClass()); | 76 | return getPluginKey(this.getClass()); | ||
77 | } | 77 | } | ||
78 | | ||||
78 | public static String getPluginKey(Class<? extends Plugin> p) { | 79 | public static String getPluginKey(Class<? extends Plugin> p) { | ||
79 | return p.getSimpleName(); | 80 | return p.getSimpleName(); | ||
80 | } | 81 | } | ||
81 | 82 | | |||
82 | /** | 83 | /** | ||
83 | * Return the human-readable plugin name. This function can | 84 | * Return the human-readable plugin name. This function can | ||
84 | * access this.context to provide translated text. | 85 | * access this.context to provide translated text. | ||
85 | */ | 86 | */ | ||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Line(s) | |||||
141 | */ | 142 | */ | ||
142 | public boolean hasMainActivity() { | 143 | public boolean hasMainActivity() { | ||
143 | return false; | 144 | return false; | ||
144 | } | 145 | } | ||
145 | 146 | | |||
146 | /** | 147 | /** | ||
147 | * Implement here what your plugin should do when clicked | 148 | * Implement here what your plugin should do when clicked | ||
148 | */ | 149 | */ | ||
149 | public void startMainActivity(Activity parentActivity) { } | 150 | public void startMainActivity(Activity parentActivity) { | ||
151 | } | ||||
150 | 152 | | |||
151 | /** | 153 | /** | ||
152 | * Return true if the entry for this app should appear in the context menu instead of the main view | 154 | * Return true if the entry for this app should appear in the context menu instead of the main view | ||
153 | */ | 155 | */ | ||
154 | public boolean displayInContextMenu() { | 156 | public boolean displayInContextMenu() { | ||
155 | return false; | 157 | return false; | ||
156 | } | 158 | } | ||
157 | 159 | | |||
158 | /** | 160 | /** | ||
159 | * Initialize the listeners and structures in your plugin. | 161 | * Initialize the listeners and structures in your plugin. | ||
160 | * Should return true if initialization was successful. | 162 | * Should return true if initialization was successful. | ||
161 | */ | 163 | */ | ||
162 | public boolean onCreate() { | 164 | public boolean onCreate() { | ||
163 | return true; | 165 | return true; | ||
164 | } | 166 | } | ||
165 | 167 | | |||
166 | /** | 168 | /** | ||
167 | * Finish any ongoing operations, remove listeners... so | 169 | * Finish any ongoing operations, remove listeners... so | ||
168 | * this object could be garbage collected. | 170 | * this object could be garbage collected. | ||
169 | */ | 171 | */ | ||
170 | public void onDestroy() { } | 172 | public void onDestroy() { | ||
173 | } | ||||
171 | 174 | | |||
172 | /** | 175 | /** | ||
173 | * Called when a plugin receives a package. By convention we return true | 176 | * Called when a plugin receives a package. By convention we return true | ||
174 | * when we have done something in response to the package or false | 177 | * when we have done something in response to the package or false | ||
175 | * otherwise, even though that value is unused as of now. | 178 | * otherwise, even though that value is unused as of now. | ||
176 | */ | 179 | */ | ||
177 | public boolean onPackageReceived(NetworkPackage np) { return false; } | 180 | public boolean onPackageReceived(NetworkPackage np) { | ||
181 | return false; | ||||
182 | } | ||||
178 | 183 | | |||
179 | /** | 184 | /** | ||
180 | * Should return the list of NetworkPackage types that this plugin can handle | 185 | * Should return the list of NetworkPackage types that this plugin can handle | ||
181 | */ | 186 | */ | ||
182 | public abstract String[] getSupportedPackageTypes(); | 187 | public abstract String[] getSupportedPackageTypes(); | ||
183 | 188 | | |||
184 | /** | 189 | /** | ||
185 | * Should return the list of NetworkPackage types that this plugin can send | 190 | * Should return the list of NetworkPackage types that this plugin can send | ||
Show All 30 Lines | |||||
216 | 221 | | |||
217 | //Permission from Manifest.permission.* | 222 | //Permission from Manifest.permission.* | ||
218 | protected boolean isPermissionGranted(String permission) { | 223 | protected boolean isPermissionGranted(String permission) { | ||
219 | int result = ContextCompat.checkSelfPermission(context, permission); | 224 | int result = ContextCompat.checkSelfPermission(context, permission); | ||
220 | return (result == PackageManager.PERMISSION_GRANTED); | 225 | return (result == PackageManager.PERMISSION_GRANTED); | ||
221 | } | 226 | } | ||
222 | 227 | | |||
223 | protected boolean arePermissionsGranted(String[] permissions) { | 228 | protected boolean arePermissionsGranted(String[] permissions) { | ||
224 | for(String permission: permissions){ | 229 | for (String permission : permissions) { | ||
225 | if(!isPermissionGranted(permission)){ | 230 | if (!isPermissionGranted(permission)) { | ||
226 | return false; | 231 | return false; | ||
227 | } | 232 | } | ||
228 | } | 233 | } | ||
229 | return true; | 234 | return true; | ||
230 | } | 235 | } | ||
231 | 236 | | |||
232 | protected AlertDialog requestPermissionDialog(Activity activity, String permissions, @StringRes int reason) { | 237 | protected AlertDialog requestPermissionDialog(Activity activity, String permissions, @StringRes int reason) { | ||
233 | return requestPermissionDialog(activity, new String[]{permissions}, reason); | 238 | return requestPermissionDialog(activity, new String[]{permissions}, reason); | ||
234 | } | 239 | } | ||
235 | 240 | | |||
236 | protected AlertDialog requestPermissionDialog(final Activity activity, final String[] permissions, @StringRes int reason){ | 241 | protected AlertDialog requestPermissionDialog(final Activity activity, final String[] permissions, @StringRes int reason) { | ||
237 | return new AlertDialog.Builder(activity) | 242 | return new AlertDialog.Builder(activity) | ||
238 | .setTitle(getDisplayName()) | 243 | .setTitle(getDisplayName()) | ||
239 | .setMessage(reason) | 244 | .setMessage(reason) | ||
240 | .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { | 245 | .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { | ||
241 | @Override | 246 | @Override | ||
242 | public void onClick(DialogInterface dialogInterface, int i) { | 247 | public void onClick(DialogInterface dialogInterface, int i) { | ||
243 | ActivityCompat.requestPermissions(activity, permissions, 0); | 248 | ActivityCompat.requestPermissions(activity, permissions, 0); | ||
244 | } | 249 | } | ||
245 | }) | 250 | }) | ||
246 | .setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() { | 251 | .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { | ||
247 | @Override | 252 | @Override | ||
248 | public void onClick(DialogInterface dialogInterface, int i) { | 253 | public void onClick(DialogInterface dialogInterface, int i) { | ||
249 | //Do nothing | 254 | //Do nothing | ||
250 | } | 255 | } | ||
251 | }) | 256 | }) | ||
252 | .create(); | 257 | .create(); | ||
253 | } | 258 | } | ||
254 | 259 | | |||
255 | /** | 260 | /** | ||
256 | * If onCreate returns false, should create a dialog explaining | 261 | * If onCreate returns false, should create a dialog explaining | ||
257 | * the problem (and how to fix it, if possible) to the user. | 262 | * the problem (and how to fix it, if possible) to the user. | ||
258 | */ | 263 | */ | ||
259 | 264 | | |||
260 | public AlertDialog getErrorDialog(Activity deviceActivity) { | 265 | public AlertDialog getErrorDialog(Activity deviceActivity) { | ||
261 | return null; | 266 | return null; | ||
262 | } | 267 | } | ||
263 | 268 | | |||
264 | public AlertDialog getPermissionExplanationDialog(Activity deviceActivity) { | 269 | public AlertDialog getPermissionExplanationDialog(Activity deviceActivity) { | ||
265 | return requestPermissionDialog(deviceActivity,getRequiredPermissions(), permissionExplanation); | 270 | return requestPermissionDialog(deviceActivity, getRequiredPermissions(), permissionExplanation); | ||
266 | } | 271 | } | ||
267 | 272 | | |||
268 | public AlertDialog getOptionalPermissionExplanationDialog(Activity deviceActivity) { | 273 | public AlertDialog getOptionalPermissionExplanationDialog(Activity deviceActivity) { | ||
269 | return requestPermissionDialog(deviceActivity,getOptionalPermissions(), optionalPermissionExplanation); | 274 | return requestPermissionDialog(deviceActivity, getOptionalPermissions(), optionalPermissionExplanation); | ||
270 | } | 275 | } | ||
271 | 276 | | |||
272 | public boolean checkRequiredPermissions(){ | 277 | public boolean checkRequiredPermissions() { | ||
273 | return arePermissionsGranted(getRequiredPermissions()); | 278 | return arePermissionsGranted(getRequiredPermissions()); | ||
274 | } | 279 | } | ||
275 | 280 | | |||
276 | public boolean checkOptionalPermissions(){ | 281 | public boolean checkOptionalPermissions() { | ||
277 | return arePermissionsGranted(getOptionalPermissions()); | 282 | return arePermissionsGranted(getOptionalPermissions()); | ||
278 | } | 283 | } | ||
279 | 284 | | |||
280 | public int getMinSdk() { | 285 | public int getMinSdk() { | ||
281 | return Build.VERSION_CODES.BASE; | 286 | return Build.VERSION_CODES.BASE; | ||
282 | } | 287 | } | ||
283 | 288 | | |||
284 | } | 289 | } |