io.appium.settings 4.2.0 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/apks/settings_apk-debug.apk +0 -0
  2. package/package.json +2 -8
  3. package/app/build.gradle +0 -38
  4. package/app/src/main/AndroidManifest.xml +0 -149
  5. package/app/src/main/java/io/appium/settings/AppiumIME.java +0 -153
  6. package/app/src/main/java/io/appium/settings/ForegroundService.java +0 -70
  7. package/app/src/main/java/io/appium/settings/LocationService.java +0 -215
  8. package/app/src/main/java/io/appium/settings/LocationTracker.java +0 -313
  9. package/app/src/main/java/io/appium/settings/NLService.java +0 -134
  10. package/app/src/main/java/io/appium/settings/Settings.java +0 -260
  11. package/app/src/main/java/io/appium/settings/UnicodeIME.java +0 -187
  12. package/app/src/main/java/io/appium/settings/Unlock.java +0 -75
  13. package/app/src/main/java/io/appium/settings/handlers/AbstractSettingHandler.java +0 -64
  14. package/app/src/main/java/io/appium/settings/handlers/AnimationSettingHandler.java +0 -66
  15. package/app/src/main/java/io/appium/settings/handlers/BluetoothConnectionSettingHandler.java +0 -42
  16. package/app/src/main/java/io/appium/settings/handlers/DataConnectionSettingHandler.java +0 -134
  17. package/app/src/main/java/io/appium/settings/handlers/LocaleSettingHandler.java +0 -86
  18. package/app/src/main/java/io/appium/settings/handlers/WiFiConnectionSettingHandler.java +0 -40
  19. package/app/src/main/java/io/appium/settings/helpers/NotificationHelpers.java +0 -67
  20. package/app/src/main/java/io/appium/settings/helpers/PlayServicesHelpers.java +0 -30
  21. package/app/src/main/java/io/appium/settings/helpers/Utils.java +0 -29
  22. package/app/src/main/java/io/appium/settings/location/FusedLocationProvider.java +0 -99
  23. package/app/src/main/java/io/appium/settings/location/LocationBuilder.java +0 -80
  24. package/app/src/main/java/io/appium/settings/location/LocationManagerProvider.java +0 -97
  25. package/app/src/main/java/io/appium/settings/location/MockLocationProvider.java +0 -31
  26. package/app/src/main/java/io/appium/settings/notifications/StoredNotification.java +0 -103
  27. package/app/src/main/java/io/appium/settings/notifications/StoredNotifications.java +0 -50
  28. package/app/src/main/java/io/appium/settings/receivers/AbstractSettingReceiver.java +0 -62
  29. package/app/src/main/java/io/appium/settings/receivers/AnimationSettingReceiver.java +0 -40
  30. package/app/src/main/java/io/appium/settings/receivers/BluetoothConnectionSettingReceiver.java +0 -49
  31. package/app/src/main/java/io/appium/settings/receivers/ClipboardReceiver.java +0 -91
  32. package/app/src/main/java/io/appium/settings/receivers/DataConnectionSettingReceiver.java +0 -40
  33. package/app/src/main/java/io/appium/settings/receivers/HasAction.java +0 -21
  34. package/app/src/main/java/io/appium/settings/receivers/LocaleSettingReceiver.java +0 -127
  35. package/app/src/main/java/io/appium/settings/receivers/LocationInfoReceiver.java +0 -67
  36. package/app/src/main/java/io/appium/settings/receivers/MediaScannerReceiver.java +0 -91
  37. package/app/src/main/java/io/appium/settings/receivers/NotificationsReceiver.java +0 -100
  38. package/app/src/main/java/io/appium/settings/receivers/SmsReader.java +0 -113
  39. package/app/src/main/java/io/appium/settings/receivers/UnpairBluetoothDevicesReceiver.java +0 -84
  40. package/app/src/main/java/io/appium/settings/receivers/WiFiConnectionSettingReceiver.java +0 -39
  41. package/app/src/main/java/io/appium/settings/recorder/RecorderConstant.java +0 -94
  42. package/app/src/main/java/io/appium/settings/recorder/RecorderService.java +0 -202
  43. package/app/src/main/java/io/appium/settings/recorder/RecorderThread.java +0 -459
  44. package/app/src/main/java/io/appium/settings/recorder/RecorderUtil.java +0 -298
  45. package/app/src/main/res/drawable-hdpi/ic_launcher.png +0 -0
  46. package/app/src/main/res/drawable-ldpi/ic_launcher.png +0 -0
  47. package/app/src/main/res/drawable-mdpi/ic_launcher.png +0 -0
  48. package/app/src/main/res/drawable-xhdpi/ic_launcher.png +0 -0
  49. package/app/src/main/res/drawable-xxhdpi/ic_launcher.png +0 -0
  50. package/app/src/main/res/drawable-xxxhdpi/ic_launcher.png +0 -0
  51. package/app/src/main/res/layout/main.xml +0 -29
  52. package/app/src/main/res/values/dimens.xml +0 -7
  53. package/app/src/main/res/values/strings.xml +0 -9
  54. package/app/src/main/res/xml/method.xml +0 -3
  55. package/build.gradle +0 -17
  56. package/gradle/wrapper/gradle-wrapper.jar +0 -0
  57. package/gradle/wrapper/gradle-wrapper.properties +0 -6
  58. package/gradle.properties +0 -15
  59. package/gradlew +0 -160
  60. package/gradlew.bat +0 -90
  61. package/settings.gradle +0 -1
@@ -1,31 +0,0 @@
1
- /*
2
- Copyright 2012-present Appium Committers
3
- <p>
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
- <p>
8
- http://www.apache.org/licenses/LICENSE-2.0
9
- <p>
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 io.appium.settings.location;
18
-
19
- import android.location.Location;
20
-
21
- public interface MockLocationProvider {
22
-
23
- void setLocation(Location location);
24
-
25
- void enable();
26
-
27
- void disable();
28
-
29
- String getProviderName();
30
-
31
- }
@@ -1,103 +0,0 @@
1
- /*
2
- Copyright 2012-present Appium Committers
3
- <p>
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
- <p>
8
- http://www.apache.org/licenses/LICENSE-2.0
9
- <p>
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 io.appium.settings.notifications;
18
-
19
- import android.os.Build;
20
- import android.os.Bundle;
21
- import android.service.notification.StatusBarNotification;
22
- import org.json.JSONException;
23
- import org.json.JSONObject;
24
-
25
- import static io.appium.settings.helpers.Utils.formatJsonNull;
26
- import static io.appium.settings.helpers.Utils.toNullableString;
27
-
28
- public class StoredNotification {
29
- private final StatusBarNotification sbn;
30
- private boolean isRemoved = false;
31
-
32
- public StoredNotification(StatusBarNotification sbn) {
33
- this.sbn = sbn;
34
- }
35
-
36
- public StatusBarNotification getNotification() {
37
- return sbn;
38
- }
39
-
40
- private void storeCharSequenceProperty(JSONObject dst, String name, String propertyName,
41
- Bundle extras) throws JSONException {
42
- CharSequence value = extras.getCharSequence(propertyName);
43
- dst.put(name, formatJsonNull(toNullableString(value)));
44
- }
45
-
46
- public JSONObject toJson() throws JSONException {
47
- JSONObject result = new JSONObject();
48
- result.put("packageName", formatJsonNull(sbn.getPackageName()));
49
- result.put("isClearable", sbn.isClearable());
50
- result.put("isOngoing", sbn.isOngoing());
51
- result.put("id", sbn.getId());
52
- result.put("tag", formatJsonNull(sbn.getTag()));
53
- result.put("postTime", sbn.getPostTime());
54
- result.put("isRemoved", isRemoved());
55
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
56
- JSONObject notification = new JSONObject();
57
- Bundle extras = sbn.getNotification().extras;
58
- storeCharSequenceProperty(notification, "title", "android.title", extras);
59
- storeCharSequenceProperty(notification, "bigTitle", "android.title.big", extras);
60
- storeCharSequenceProperty(notification, "text", "android.text", extras);
61
- storeCharSequenceProperty(notification, "bigText", "android.bigText", extras);
62
- storeCharSequenceProperty(notification, "tickerText", "android.tickerText", extras);
63
- storeCharSequenceProperty(notification, "subText", "android.subText", extras);
64
- storeCharSequenceProperty(notification, "infoText", "android.infoText", extras);
65
- storeCharSequenceProperty(notification, "template", "android.template", extras);
66
- result.put("notification", notification);
67
- } else {
68
- result.put("notification", JSONObject.NULL);
69
- }
70
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
71
- result.put("isGroup", sbn.isGroup());
72
- } else {
73
- result.put("isGroup", false);
74
- }
75
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
76
- result.put("userHandle", sbn.getUser().hashCode());
77
- result.put("groupKey", formatJsonNull(sbn.getGroupKey()));
78
- } else {
79
- //noinspection deprecation
80
- result.put("userHandle", sbn.getUserId());
81
- result.put("groupKey", JSONObject.NULL);
82
- }
83
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
84
- result.put("overrideGroupKey", formatJsonNull(sbn.getOverrideGroupKey()));
85
- } else {
86
- result.put("overrideGroupKey", JSONObject.NULL);
87
- }
88
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
89
- result.put("key", formatJsonNull(sbn.getKey()));
90
- } else {
91
- result.put("key", JSONObject.NULL);
92
- }
93
- return result;
94
- }
95
-
96
- public boolean isRemoved() {
97
- return isRemoved;
98
- }
99
-
100
- public void setRemoved(boolean removed) {
101
- this.isRemoved = removed;
102
- }
103
- }
@@ -1,50 +0,0 @@
1
- /*
2
- Copyright 2012-present Appium Committers
3
- <p>
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
- <p>
8
- http://www.apache.org/licenses/LICENSE-2.0
9
- <p>
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 io.appium.settings.notifications;
18
-
19
- import org.json.JSONArray;
20
- import org.json.JSONException;
21
-
22
- import java.util.Collections;
23
- import java.util.LinkedList;
24
- import java.util.List;
25
-
26
- public class StoredNotifications {
27
- private List<StoredNotification> notifications = Collections.emptyList();
28
-
29
- private StoredNotifications() {}
30
-
31
- private static StoredNotifications instance = null;
32
- public static synchronized StoredNotifications getInstance() {
33
- if (instance == null) {
34
- instance = new StoredNotifications();
35
- }
36
- return instance;
37
- }
38
-
39
- public void bindNotificationsBuffer(List<StoredNotification> notifications) {
40
- this.notifications = notifications;
41
- }
42
-
43
- public JSONArray getNotifications() throws JSONException {
44
- JSONArray result = new JSONArray();
45
- for (StoredNotification sn: new LinkedList<>(notifications)) {
46
- result.put(sn.toJson());
47
- }
48
- return result;
49
- }
50
- }
@@ -1,62 +0,0 @@
1
- /*
2
- Copyright 2012-present Appium Committers
3
- <p>
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
- <p>
8
- http://www.apache.org/licenses/LICENSE-2.0
9
- <p>
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 io.appium.settings.receivers;
18
-
19
- import android.app.Activity;
20
- import android.content.BroadcastReceiver;
21
- import android.content.Context;
22
- import android.content.Intent;
23
- import android.util.Log;
24
-
25
- import java.util.Arrays;
26
- import java.util.List;
27
-
28
- import io.appium.settings.handlers.AbstractSettingHandler;
29
-
30
- public abstract class AbstractSettingReceiver extends BroadcastReceiver {
31
- private static final String TAG = AbstractSettingReceiver.class.getSimpleName();
32
-
33
- private static final String COMMAND = "setstatus";
34
- private static final String COMMAND_ENABLE = "enable";
35
- private static final String COMMAND_DISABLE = "disable";
36
-
37
- // am broadcast -a io.appium.settings.[wifi|data_connection|animation|bluetooth] --es setstatus [enable|disable]
38
- @Override
39
- public void onReceive(Context context, Intent intent) {
40
- String command = intent.getStringExtra(COMMAND);
41
- List<String> supportedCommands = Arrays.asList(COMMAND_ENABLE, COMMAND_DISABLE);
42
- if (!supportedCommands.contains(command)) {
43
- Log.e(TAG, String.format("Cannot identify the command [%s]", command));
44
- setResultCode(Activity.RESULT_CANCELED);
45
- return;
46
- }
47
- boolean isSuccessful;
48
- AbstractSettingHandler handler = getHandler(context);
49
- if (handler == null) {
50
- setResultCode(Activity.RESULT_CANCELED);
51
- return;
52
- }
53
- if (command.equals(COMMAND_ENABLE)) {
54
- isSuccessful = handler.enable();
55
- } else {
56
- isSuccessful = handler.disable();
57
- }
58
- setResultCode(isSuccessful ? Activity.RESULT_OK : Activity.RESULT_CANCELED);
59
- }
60
-
61
- protected abstract AbstractSettingHandler getHandler(Context context);
62
- }
@@ -1,40 +0,0 @@
1
- /*
2
- Copyright 2012-present Appium Committers
3
- <p>
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
- <p>
8
- http://www.apache.org/licenses/LICENSE-2.0
9
- <p>
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 io.appium.settings.receivers;
18
-
19
- import android.content.Context;
20
-
21
- import io.appium.settings.handlers.AnimationSettingHandler;
22
-
23
- /**
24
- * am broadcast -a io.appium.settings.animation --es setstatus [enable|disable]
25
- */
26
- public class AnimationSettingReceiver extends AbstractSettingReceiver
27
- implements HasAction{
28
-
29
- private static final String ACTION = "io.appium.settings.animation";
30
-
31
- @Override
32
- protected AnimationSettingHandler getHandler(Context context) {
33
- return new AnimationSettingHandler(context);
34
- }
35
-
36
- @Override
37
- public String getAction() {
38
- return ACTION;
39
- }
40
- }
@@ -1,49 +0,0 @@
1
- /*
2
- Copyright 2012-present Appium Committers
3
- <p>
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
- <p>
8
- http://www.apache.org/licenses/LICENSE-2.0
9
- <p>
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 io.appium.settings.receivers;
18
-
19
- import android.bluetooth.BluetoothAdapter;
20
- import android.content.Context;
21
- import android.util.Log;
22
-
23
- import io.appium.settings.handlers.BluetoothConnectionSettingHandler;
24
-
25
- /**
26
- * am broadcast -a io.appium.settings.bluetooth --es setstatus [enable|disable]
27
- */
28
- public class BluetoothConnectionSettingReceiver extends AbstractSettingReceiver
29
- implements HasAction {
30
- private static final String TAG = BluetoothConnectionSettingReceiver.class.getSimpleName();
31
- private static final String ACTION = "io.appium.settings.bluetooth";
32
-
33
- @Override
34
- protected BluetoothConnectionSettingHandler getHandler(Context context) {
35
- BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
36
- if (bluetoothAdapter == null) {
37
- String errorMessage = "No default bluetooth adapter found";
38
- Log.e(TAG, errorMessage);
39
- setResultData(errorMessage);
40
- return null;
41
- }
42
- return new BluetoothConnectionSettingHandler(context, bluetoothAdapter);
43
- }
44
-
45
- @Override
46
- public String getAction() {
47
- return ACTION;
48
- }
49
- }
@@ -1,91 +0,0 @@
1
- /*
2
- Copyright 2012-present Appium Committers
3
- <p>
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
- <p>
8
- http://www.apache.org/licenses/LICENSE-2.0
9
- <p>
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 io.appium.settings.receivers;
18
-
19
- import android.app.Activity;
20
- import android.content.BroadcastReceiver;
21
- import android.content.ClipData;
22
- import android.content.ClipboardManager;
23
- import android.content.Context;
24
- import android.content.Intent;
25
- import android.util.Base64;
26
- import android.util.Log;
27
-
28
- import java.io.UnsupportedEncodingException;
29
-
30
- public class ClipboardReceiver extends BroadcastReceiver implements HasAction {
31
- private static final String TAG = ClipboardReceiver.class.getSimpleName();
32
-
33
- private static final String ACTION = "io.appium.settings.clipboard.get";
34
-
35
- private static String charSequenceToString(CharSequence input) {
36
- return input == null ? "" : input.toString();
37
- }
38
-
39
- private String getClipboardText(Context context) {
40
- final ClipboardManager cm = (ClipboardManager) context
41
- .getSystemService(Context.CLIPBOARD_SERVICE);
42
- if (cm == null) {
43
- Log.e(TAG, "Cannot get an instance of ClipboardManager");
44
- return null;
45
- }
46
- if (!cm.hasPrimaryClip()) {
47
- return "";
48
- }
49
- final ClipData cd = cm.getPrimaryClip();
50
- if (cd == null || cd.getItemCount() == 0) {
51
- return "";
52
- }
53
- return charSequenceToString(cd.getItemAt(0).coerceToText(context));
54
- }
55
-
56
- /**
57
- * Responds to broadcast requests like
58
- * am broadcast -a io.appium.settings.clipboard.get
59
- * with the base64-encoded current clipboard text content
60
- * or an empty string if no content has been received
61
- */
62
- @Override
63
- public void onReceive(Context context, Intent intent) {
64
- Log.d(TAG, "Getting current clipboard content");
65
- final String clipboardContent = getClipboardText(context);
66
- if (clipboardContent == null) {
67
- setResultCode(Activity.RESULT_CANCELED);
68
- setResultData("");
69
- return;
70
- }
71
-
72
- try {
73
- // TODO: Use StandardCharsets.UTF_8 after the minimum supported API version
74
- // TODO: is bumped above 18
75
- //noinspection CharsetObjectCanBeUsed
76
- String clipboardContentBase64 = Base64.encodeToString(
77
- clipboardContent.getBytes("UTF-8"), Base64.DEFAULT);
78
- setResultCode(Activity.RESULT_OK);
79
- setResultData(clipboardContentBase64);
80
- } catch (UnsupportedEncodingException e) {
81
- e.printStackTrace();
82
- setResultCode(Activity.RESULT_CANCELED);
83
- setResultData("");
84
- }
85
- }
86
-
87
- @Override
88
- public String getAction() {
89
- return ACTION;
90
- }
91
- }
@@ -1,40 +0,0 @@
1
- /*
2
- Copyright 2012-present Appium Committers
3
- <p>
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
- <p>
8
- http://www.apache.org/licenses/LICENSE-2.0
9
- <p>
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 io.appium.settings.receivers;
18
-
19
- import android.content.Context;
20
-
21
- import io.appium.settings.handlers.DataConnectionSettingHandler;
22
-
23
- /**
24
- * am broadcast -a io.appium.settings.data_connection --es setstatus [enable|disable]
25
- */
26
- public class DataConnectionSettingReceiver extends AbstractSettingReceiver
27
- implements HasAction {
28
-
29
- private static final String ACTION = "io.appium.settings.data_connection";
30
-
31
- @Override
32
- protected DataConnectionSettingHandler getHandler(Context context) {
33
- return new DataConnectionSettingHandler(context);
34
- }
35
-
36
- @Override
37
- public String getAction() {
38
- return ACTION;
39
- }
40
- }
@@ -1,21 +0,0 @@
1
- /*
2
- Copyright 2012-present Appium Committers
3
- <p>
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
- <p>
8
- http://www.apache.org/licenses/LICENSE-2.0
9
- <p>
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 io.appium.settings.receivers;
18
-
19
- public interface HasAction {
20
- String getAction();
21
- }
@@ -1,127 +0,0 @@
1
- /*
2
- Copyright 2012-present Appium Committers
3
- <p>
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
- <p>
8
- http://www.apache.org/licenses/LICENSE-2.0
9
- <p>
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 io.appium.settings.receivers;
18
-
19
- import static org.apache.commons.lang3.StringUtils.isBlank;
20
-
21
- import android.app.Activity;
22
- import android.content.BroadcastReceiver;
23
- import android.content.Context;
24
- import android.content.Intent;
25
- import android.util.Log;
26
-
27
- import org.apache.commons.lang3.LocaleUtils;
28
-
29
- import java.util.ArrayList;
30
- import java.util.List;
31
- import java.util.Locale;
32
-
33
- import io.appium.settings.handlers.LocaleSettingHandler;
34
-
35
- public class LocaleSettingReceiver extends BroadcastReceiver implements HasAction {
36
- private static final String TAG = LocaleSettingReceiver.class.getSimpleName();
37
-
38
- private static final String LANG = "lang";
39
- private static final String COUNTRY = "country";
40
- private static final String SCRIPT = "script";
41
-
42
- private static final String ACTION = "io.appium.settings.locale";
43
-
44
- // am broadcast -a io.appium.settings.locale --es lang ja --es country JP
45
- @Override
46
- public void onReceive(Context context, Intent intent) {
47
- String language = intent.getStringExtra(LANG);
48
- String country = intent.getStringExtra(COUNTRY);
49
- if (language == null || country == null) {
50
- Log.w(TAG, "It is required to provide both language and country, for example: " +
51
- "am broadcast -a io.appium.settings.locale --es lang ja --es country JP");
52
- Log.i(TAG, "Set en-US by default.");
53
- language = "en";
54
- country= "US";
55
- }
56
- // Expect https://developer.android.com/reference/java/util/Locale.html#Locale(java.lang.String,%20java.lang.String) format.
57
- Locale locale = new Locale(language, country);
58
- String script = intent.getStringExtra(SCRIPT);
59
- if (script != null) {
60
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
61
- locale = new Locale.Builder().setLocale(locale).setScript(script).build();
62
- } else {
63
- Log.w(TAG, String.format("Script value '%s' is ignored as " +
64
- "setting of it is not supported by the current Android version", script));
65
- }
66
- }
67
- if (!LocaleUtils.isAvailableLocale(locale)) {
68
- List<Locale> approximateMatchesLc = matchLocales(language, country);
69
- if (!approximateMatchesLc.isEmpty() && isBlank(script)) {
70
- Log.i(TAG, String.format(
71
- "The locale %s is not known. Selecting the closest known one %s instead",
72
- locale, approximateMatchesLc.get(0))
73
- );
74
- locale = approximateMatchesLc.get(0);
75
- } else {
76
- List<Locale> approximateMatchesL = matchLocales(language);
77
- if (approximateMatchesL.isEmpty()) {
78
- Log.e(TAG, String.format(
79
- "The locale %s is not known. Only the following locales are available: %s",
80
- locale, LocaleUtils.availableLocaleList())
81
- );
82
- } else {
83
- Log.e(TAG, String.format(
84
- "The locale %s is not known. " +
85
- "The following locales are available for the %s language: %s" +
86
- "The following locales are available altogether: %s",
87
- locale, language, approximateMatchesL, LocaleUtils.availableLocaleList())
88
- );
89
- }
90
- setResultCode(Activity.RESULT_CANCELED);
91
- setResultData(String.format("The locale %s is not known", locale));
92
- return;
93
- }
94
- }
95
-
96
- new LocaleSettingHandler(context).setLocale(locale);
97
- Log.i(TAG, String.format("Set locale: %s", locale));
98
- setResultCode(Activity.RESULT_OK);
99
- setResultData(locale.toString());
100
- }
101
-
102
- @Override
103
- public String getAction() {
104
- return ACTION;
105
- }
106
-
107
- private static List<Locale> matchLocales(String language) {
108
- List<Locale> matches = new ArrayList<>();
109
- for (Locale locale: LocaleUtils.availableLocaleList()) {
110
- if (locale.getLanguage().equalsIgnoreCase(language)) {
111
- matches.add(locale);
112
- }
113
- }
114
- return matches;
115
- }
116
-
117
- private static List<Locale> matchLocales(String language, String country) {
118
- List<Locale> matches = new ArrayList<>();
119
- for (Locale locale: LocaleUtils.availableLocaleList()) {
120
- if (locale.getLanguage().equalsIgnoreCase(language)
121
- && locale.getCountry().equalsIgnoreCase(country)) {
122
- matches.add(locale);
123
- }
124
- }
125
- return matches;
126
- }
127
- }