Commit fcff6d6d authored by Jeremy JAMET's avatar Jeremy JAMET

Fix ActivityNotFound Exception

parent db70bcd0
package com.kunzisoft.keyboard.switcher.settings;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import androidx.annotation.ColorInt;
import androidx.annotation.RequiresApi;
import androidx.preference.SwitchPreference;
import androidx.preference.Preference;
import com.kunzisoft.androidclearchroma.ChromaPreferenceFragmentCompat;
import com.kunzisoft.keyboard.switcher.KeyboardNotificationService;
......@@ -17,6 +15,12 @@ import com.kunzisoft.keyboard.switcher.R;
import com.kunzisoft.keyboard.switcher.dialogs.WarningFloatingButtonDialog;
import com.kunzisoft.keyboard.switcher.utils.Utilities;
import androidx.annotation.ColorInt;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;
import androidx.preference.SwitchPreference;
public class PreferenceFragment extends ChromaPreferenceFragmentCompat
implements Preference.OnPreferenceClickListener,
Preference.OnPreferenceChangeListener {
......@@ -132,11 +136,21 @@ public class PreferenceFragment extends ChromaPreferenceFragmentCompat
/* check if we already have permission to draw over other apps */
if (getActivity() != null
&& !Settings.canDrawOverlays(getActivity())) {
/* if not construct intent to request permission */
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getActivity().getPackageName()));
/* request permission via start activity for result */
startActivityForResult(intent, REQUEST_CODE);
try {
/* if not construct intent to request permission */
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getActivity().getPackageName()));
/* request permission via start activity for result */
startActivityForResult(intent, REQUEST_CODE);
} catch (ActivityNotFoundException e) {
if (getContext() != null)
new AlertDialog.Builder(getContext())
.setMessage(R.string.error_overlay_permission_request)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {}
}).create().show();
}
} else {
startFloatingButtonService();
}
......
package com.kunzisoft.keyboard.switcher.utils;
import android.app.PendingIntent;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.provider.Settings;
import androidx.annotation.Nullable;
import android.view.inputmethod.InputMethodManager;
import com.kunzisoft.keyboard.switcher.KeyboardManagerActivity;
import com.kunzisoft.keyboard.switcher.R;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
......@@ -15,9 +20,18 @@ public class Utilities {
public static void openAvailableKeyboards(@Nullable Context context) {
if (context != null) {
Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
try {
Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
new AlertDialog.Builder(context)
.setMessage(R.string.error_unavailable_keyboard_feature)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {}
}).create().show();
}
}
}
......
......@@ -17,6 +17,8 @@
<string name="settings_ime_change_key" translatable="false">settings_ime_choose_key</string>
<string name="settings_ime_change_title">Change the default keyboard</string>
<string name="error_unavailable_keyboard_feature">Your system is not able to open the keyboard selection dialog.</string>
<string name="startup">Startup</string>
<string name="settings_launch_startup_key" translatable="false">settings_launch_startup</string>
......@@ -34,6 +36,8 @@
<string name="settings_floating_button_title">Enable floating button</string>
<string name="settings_floating_button_description">Enable a floating button to choose a keyboard</string>
<string name="error_overlay_permission_request">Your system is not able to open the overlay permission request dialog.</string>
<string name="floating_button_warning">This feature may be unstable in last versions of Android (Lollipop and following).\n\nThe floating button may disappear if the system does not fully accept the \"Allow display over the app\" feature.\nIf so, try updating the Android version of your device. </string>
<string name="floating_button_above_screen">You must accept the permissions to permit the floating button to be drawing over other apps.</string>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment