123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- package com.romellfudi.ussdlibrary;
- import android.accessibilityservice.AccessibilityServiceInfo;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.pm.ApplicationInfo;
- import android.net.Uri;
- import android.os.Build;
- import android.provider.Settings;
- import android.telecom.PhoneAccountHandle;
- import android.telecom.TelecomManager;
- import android.telephony.SubscriptionManager;
- import android.telephony.TelephonyManager;
- import android.text.TextUtils;
- import android.view.accessibility.AccessibilityManager;
- import android.widget.Toast;
- import com.elvishew.xlog.XLog;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.List;
- public class USSDController implements USSDInterface, USSDApi {
- protected static USSDController instance;
- protected Context context;
- protected HashMap<String, HashSet<String>> map;
- protected CallbackInvoke callbackInvoke;
- protected CallbackMessage callbackMessage;
- protected static final String KEY_LOGIN = "KEY_LOGIN";
- protected static final String KEY_ERROR = "KEY_ERROR";
- protected Boolean isRunning = false;
- protected Boolean send = false;
- private USSDInterface ussdInterface;
-
- public static USSDController getInstance(Context context) {
- if (instance == null)
- instance = new USSDController(context);
- return instance;
- }
- private USSDController(Context context) {
- ussdInterface = this;
- this.context = context;
- }
-
- public void callUSSDInvoke(String ussdPhoneNumber, HashMap<String, HashSet<String>> map, CallbackInvoke callbackInvoke) {
- XLog.i("=====================callUSSDInvoke=====================");
- callUSSDInvoke(ussdPhoneNumber, 0, map, callbackInvoke);
- }
-
- public void callUSSDOverlayInvoke(String ussdPhoneNumber, HashMap<String, HashSet<String>> map, CallbackInvoke callbackInvoke) {
- XLog.i("=====================callUSSDOverlayInvoke=====================");
- callUSSDOverlayInvoke(ussdPhoneNumber, 0, map, callbackInvoke);
- }
-
- @SuppressLint("MissingPermission")
- public void callUSSDInvoke(String ussdPhoneNumber, int subId, HashMap<String, HashSet<String>> map, CallbackInvoke callbackInvoke) {
- send = false;
- this.callbackInvoke = callbackInvoke;
- this.map = map;
- if (verifyAccesibilityAccess(context)) {
- dialUp(ussdPhoneNumber, subId);
- } else {
- this.callbackInvoke.over("Check your accessibility");
- }
- }
-
- @SuppressLint("MissingPermission")
- public void callUSSDOverlayInvoke(String ussdPhoneNumber, int subId, HashMap<String, HashSet<String>> map, CallbackInvoke callbackInvoke) {
- send = false;
- this.callbackInvoke = callbackInvoke;
- this.map = map;
- if (verifyAccesibilityAccess(context) && verifyOverLay(context)) {
- dialUp(ussdPhoneNumber, subId);
- } else if (!verifyAccesibilityAccess(context)) {
- this.callbackInvoke.over("Check your accessibility -> error");
- } else if (!verifyOverLay(context)) {
- this.callbackInvoke.over("overlay permission -> error");
- } else {
- this.callbackInvoke.over("other -> error");
- }
- }
- private void dialUp(String ussdPhoneNumber, int subId) {
- if (map == null || (!map.containsKey(KEY_ERROR) || !map.containsKey(KEY_LOGIN))) {
- this.callbackInvoke.over("Bad Mapping structure");
- return;
- }
- if (ussdPhoneNumber.isEmpty()) {
- this.callbackInvoke.over("Bad ussd number");
- return;
- }
- String uri = Uri.encode("#");
- if (uri != null)
- ussdPhoneNumber = ussdPhoneNumber.replace("#", uri);
- Uri uriPhone = Uri.parse("tel:" + ussdPhoneNumber);
- if (uriPhone != null)
- isRunning = true;
- Intent intent = getActionCallIntent(uriPhone, subId);
- if (intent != null)
- this.context.startActivity(getActionCallIntent(uriPhone, subId));
- }
-
- @SuppressLint("MissingPermission")
- private Intent getActionCallIntent(Uri uri, int subId) {
- Intent intent = new Intent(Intent.ACTION_CALL, uri);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent.putExtra("com.android.phone.force.slot", true);
- intent.putExtra("Cdma_Supp", true);
- PhoneAccountHandle phoneAccountHandle = null;
- int slotIndex = -1;
- TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
- if (telecomManager != null) {
- List<PhoneAccountHandle> phoneAccountHandleList = telecomManager.getCallCapablePhoneAccounts();
- TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
- if (phoneAccountHandleList != null) {
- XLog.i("phoneAccountHandleList.size() == " + phoneAccountHandleList.size());
- }
- for (int i = 0; i < phoneAccountHandleList.size(); i++) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
- if (subId == telephonyManager.getSubscriptionId(phoneAccountHandleList.get(i))) {
- XLog.i("subId 匹配成功, subId == " + subId);
- phoneAccountHandle = phoneAccountHandleList.get(i);
- slotIndex = SubscriptionManager.getSlotIndex(subId);
- break;
- } else {
- XLog.i("subId == " + subId + ", telephonyManager.getSubscriptionId(phoneAccountHandleList.get(i)) == " + telephonyManager.getSubscriptionId(phoneAccountHandleList.get(i)));
- }
- }
- }
- if (phoneAccountHandle != null) {
- intent.putExtra("android.telecom.extra.PHONE_ACCOUNT_HANDLE", phoneAccountHandle);
- }
- if (phoneAccountHandle == null) {
- if (callbackInvoke != null) {
- callbackInvoke.over("phoneAccountHandle == " + phoneAccountHandle);
- }
- return null;
- }
- }
- if (slotIndex == -1) {
- if (callbackInvoke != null) {
- callbackInvoke.over("slotIndex == " + slotIndex);
- }
- return null;
- }
- final String simSlotName[] = {
- "extra_asus_dial_use_dualsim",
- "com.android.phone.extra.slot",
- "slot",
- "simslot",
- "sim_slot",
- "subscription",
- "Subscription",
- "phone",
- "com.android.phone.DialingMode",
- "simSlot",
- "slot_id",
- "simId",
- "simnum",
- "phone_type",
- "slotId",
- "slotIdx"
- };
- for (String s : simSlotName)
- intent.putExtra(s, slotIndex);
- return intent;
- }
-
- public void sendData(String text) {
- USSDService.send(text);
- }
-
- public void send(String text, CallbackMessage callbackMessage) {
- this.callbackMessage = callbackMessage;
- this.send = true;
- ussdInterface.sendData(text);
- }
-
- @Override
- public void cancel() {
- USSDService.cancel();
- }
-
- public static boolean verifyAccesibilityAccess(Context context) {
- boolean isEnabled = USSDController.isAccessiblityServicesEnable(context);
- if (!isEnabled) {
- if (context instanceof Activity) {
- openSettingsAccessibility((Activity) context);
- } else {
- Toast.makeText(context, R.string.accessibility_service_is_not_enabled, Toast.LENGTH_LONG).show();
- }
- }
- return isEnabled;
- }
-
- public static boolean verifyOverLay(Context context) {
- boolean m_android_doesnt_grant = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
- && !Settings.canDrawOverlays(context);
- if (m_android_doesnt_grant) {
- if (context instanceof Activity) {
- openSettingsOverlay((Activity) context);
- } else {
- Toast.makeText(context, R.string.overlay_permission_have_not_grant_permission, Toast.LENGTH_LONG).show();
- }
- return false;
- } else
- return true;
- }
- private static void openSettingsAccessibility(final Activity activity) {
- AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
- alertDialogBuilder.setTitle(R.string.tip1);
- ApplicationInfo applicationInfo = activity.getApplicationInfo();
- int stringId = applicationInfo.labelRes;
- String name = applicationInfo.labelRes == 0 ?
- applicationInfo.nonLocalizedLabel.toString() : activity.getString(stringId);
- alertDialogBuilder.setMessage("Accessibility permission must be granted to use " + name);
- alertDialogBuilder.setCancelable(false);
- alertDialogBuilder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- activity.startActivityForResult(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS), 1);
- }
- });
- alertDialogBuilder.setNeutralButton("Close", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- dialog.dismiss();
- }
- });
- AlertDialog alertDialog = alertDialogBuilder.create();
- if (alertDialog != null) {
- alertDialog.show();
- }
- }
- private static void openSettingsOverlay(final Activity activity) {
- AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
- alertDialogBuilder.setTitle(R.string.tip2);
- ApplicationInfo applicationInfo = activity.getApplicationInfo();
- int stringId = applicationInfo.labelRes;
- String name = applicationInfo.labelRes == 0 ?
- applicationInfo.nonLocalizedLabel.toString() : activity.getString(stringId);
- alertDialogBuilder
- .setMessage("You must allow " + name + " to appear on top of other applications");
- alertDialogBuilder.setCancelable(false);
- alertDialogBuilder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
- Uri.parse("package:" + activity.getPackageName()));
- activity.startActivity(intent);
- }
- });
- alertDialogBuilder.setNeutralButton("Close", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- dialog.dismiss();
- }
- });
- AlertDialog alertDialog = alertDialogBuilder.create();
- if (alertDialog != null) {
- alertDialog.show();
- }
- }
- protected static boolean isAccessiblityServicesEnable(Context context) {
- AccessibilityManager am = (AccessibilityManager) context
- .getSystemService(Context.ACCESSIBILITY_SERVICE);
- if (am != null) {
- for (AccessibilityServiceInfo service : am.getInstalledAccessibilityServiceList()) {
- if (service.getId().contains(context.getPackageName())) {
- return USSDController.isAccessibilitySettingsOn(context, service.getId());
- }
- }
- }
- return false;
- }
- protected static boolean isAccessibilitySettingsOn(Context context, final String service) {
- int accessibilityEnabled = 0;
- try {
- accessibilityEnabled = Settings.Secure.getInt(
- context.getApplicationContext().getContentResolver(),
- android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
- } catch (Settings.SettingNotFoundException e) {
-
- }
- if (accessibilityEnabled == 1) {
- String settingValue = Settings.Secure.getString(
- context.getApplicationContext().getContentResolver(),
- Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
- if (settingValue != null) {
- TextUtils.SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter(':');
- splitter.setString(settingValue);
- while (splitter.hasNext()) {
- String accessabilityService = splitter.next();
- if (accessabilityService.equalsIgnoreCase(service)) {
- return true;
- }
- }
- }
- }
- return false;
- }
- public interface CallbackInvoke {
- void responseInvoke(String message);
- void over(String message);
- }
- public interface CallbackMessage {
- void responseMessage(String message);
- void over(String message);
- }
- }
|