diff options
Diffstat (limited to 'app/src/main')
19 files changed, 134 insertions, 374 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java index bb2e43de..3db6bf20 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java @@ -275,10 +275,7 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA }); binding.commentCommentEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), binding.commentCommentEditText); } private void loadCurrentAccount() { @@ -345,10 +342,7 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA @Override protected void onPause() { super.onPause(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.hideSoftInputFromWindow(binding.commentCommentEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java index 3aeb102a..c2dc2afa 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java @@ -55,6 +55,7 @@ import ml.docilealligator.infinityforreddit.customtheme.CustomThemeViewModel; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils; +import ml.docilealligator.infinityforreddit.utils.Utils; public class CustomThemeListingActivity extends BaseActivity implements CustomThemeOptionsBottomSheetFragment.CustomThemeOptionsBottomSheetFragmentListener, @@ -166,26 +167,19 @@ public class CustomThemeListingActivity extends BaseActivity implements EditText themeNameEditText = dialogView.findViewById(R.id.name_edit_text_edit_name_dialog); themeNameEditText.setText(oldThemeName); themeNameEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), themeNameEditText); new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) .setTitle(R.string.edit_theme_name) .setView(dialogView) .setPositiveButton(R.string.ok, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(themeNameEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); ChangeThemeName.changeThemeName(executor, redditDataRoomDatabase, oldThemeName, themeNameEditText.getText().toString()); }) .setNegativeButton(R.string.cancel, null) .setOnDismissListener(dialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(themeNameEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .show(); } @@ -311,18 +305,13 @@ public class CustomThemeListingActivity extends BaseActivity implements EditText themeNameEditText = dialogView.findViewById(R.id.name_edit_text_edit_name_dialog); themeNameEditText.setText(customTheme.name); themeNameEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(CustomThemeListingActivity.this, new Handler(), themeNameEditText); new MaterialAlertDialogBuilder(CustomThemeListingActivity.this, R.style.MaterialAlertDialogTheme) .setTitle(R.string.edit_theme_name) .setView(dialogView) .setPositiveButton(R.string.ok, (editTextDialogInterface, i1) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(themeNameEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(CustomThemeListingActivity.this); if (!themeNameEditText.getText().toString().equals("")) { customTheme.name = themeNameEditText.getText().toString(); } @@ -330,9 +319,7 @@ public class CustomThemeListingActivity extends BaseActivity implements }) .setNegativeButton(R.string.cancel, null) .setOnDismissListener(editTextDialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(themeNameEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(CustomThemeListingActivity.this); }) .show(); }) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java index 29f2f0f1..661c568d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditCommentActivity.java @@ -1,17 +1,16 @@ package ml.docilealligator.infinityforreddit.activities; import android.content.ActivityNotFoundException; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Environment; +import android.os.Handler; import android.provider.MediaStore; import android.view.Menu; import android.view.MenuItem; -import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.Toast; @@ -164,10 +163,7 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab markdownBottomBarRecyclerView.setAdapter(adapter); contentEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), contentEditText); } @Override @@ -194,10 +190,7 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab @Override protected void onPause() { super.onPause(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.hideSoftInputFromWindow(contentEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditPostActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditPostActivity.java index 2bdcdd0b..c0960421 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditPostActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditPostActivity.java @@ -8,6 +8,7 @@ import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Environment; +import android.os.Handler; import android.provider.MediaStore; import android.view.Menu; import android.view.MenuItem; @@ -173,10 +174,7 @@ public class EditPostActivity extends BaseActivity implements UploadImageEnabled markdownBottomBarRecyclerView.setAdapter(adapter); contentEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), contentEditText); } @Override @@ -208,10 +206,7 @@ public class EditPostActivity extends BaseActivity implements UploadImageEnabled @Override protected void onPause() { super.onPause(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.hideSoftInputFromWindow(contentEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java index ab7c766e..eed1d64f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java @@ -1,6 +1,5 @@ package ml.docilealligator.infinityforreddit.activities; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; @@ -12,7 +11,6 @@ import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.inputmethod.EditorInfo; -import android.view.inputmethod.InputMethodManager; import android.widget.Toast; import androidx.annotation.NonNull; @@ -181,15 +179,10 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf View rootView = getLayoutInflater().inflate(R.layout.dialog_go_to_thing_edit_text, mCoordinatorLayout, false); TextInputEditText thingEditText = rootView.findViewById(R.id.text_input_edit_text_go_to_thing_edit_text); thingEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), thingEditText); thingEditText.setOnEditorActionListener((textView, i, keyEvent) -> { if (i == EditorInfo.IME_ACTION_DONE) { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent pmIntent = new Intent(this, SendPrivateMessageActivity.class); pmIntent.putExtra(SendPrivateMessageActivity.EXTRA_RECIPIENT_USERNAME, thingEditText.getText().toString()); startActivity(pmIntent); @@ -202,27 +195,20 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf .setView(rootView) .setPositiveButton(R.string.ok, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent pmIntent = new Intent(this, SendPrivateMessageActivity.class); pmIntent.putExtra(SendPrivateMessageActivity.EXTRA_RECIPIENT_USERNAME, thingEditText.getText().toString()); startActivity(pmIntent); }) .setNegativeButton(R.string.cancel, null) .setNeutralButton(R.string.search, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } - + Utils.hideKeyboard(this); Intent intent = new Intent(this, SearchActivity.class); intent.putExtra(SearchActivity.EXTRA_SEARCH_ONLY_USERS, true); startActivityForResult(intent, SEARCH_USER_REQUEST_CODE); }) .setOnDismissListener(dialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .show(); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java index 7ae7d87e..5744b707 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -1395,12 +1395,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb View rootView = getLayoutInflater().inflate(R.layout.dialog_go_to_thing_edit_text, coordinatorLayout, false); TextInputEditText thingEditText = rootView.findViewById(R.id.text_input_edit_text_go_to_thing_edit_text); RecyclerView recyclerView = rootView.findViewById(R.id.recycler_view_go_to_thing_edit_text); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); SubredditAutocompleteRecyclerViewAdapter adapter = new SubredditAutocompleteRecyclerViewAdapter( this, mCustomThemeWrapper, subredditData -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent intent = new Intent(MainActivity.this, ViewSubredditDetailActivity.class); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subredditData.getName()); startActivity(intent); @@ -1408,14 +1405,10 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb recyclerView.setAdapter(adapter); thingEditText.requestFocus(); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), thingEditText); thingEditText.setOnEditorActionListener((textView, i, keyEvent) -> { if (i == EditorInfo.IME_ACTION_DONE) { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent subredditIntent = new Intent(this, ViewSubredditDetailActivity.class); subredditIntent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, thingEditText.getText().toString()); startActivity(subredditIntent); @@ -1473,22 +1466,16 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb .setView(rootView) .setPositiveButton(R.string.ok, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent subredditIntent = new Intent(this, ViewSubredditDetailActivity.class); subredditIntent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, thingEditText.getText().toString()); startActivity(subredditIntent); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .setOnDismissListener(dialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .show(); } @@ -1497,15 +1484,10 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb View rootView = getLayoutInflater().inflate(R.layout.dialog_go_to_thing_edit_text, coordinatorLayout, false); TextInputEditText thingEditText = rootView.findViewById(R.id.text_input_edit_text_go_to_thing_edit_text); thingEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), thingEditText); thingEditText.setOnEditorActionListener((textView, i, keyEvent) -> { if (i == EditorInfo.IME_ACTION_DONE) { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent userIntent = new Intent(this, ViewUserDetailActivity.class); userIntent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, thingEditText.getText().toString()); startActivity(userIntent); @@ -1518,22 +1500,16 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb .setView(rootView) .setPositiveButton(R.string.ok, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent userIntent = new Intent(this, ViewUserDetailActivity.class); userIntent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, thingEditText.getText().toString()); startActivity(userIntent); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .setOnDismissListener(dialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .show(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterUsageListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterUsageListingActivity.java index 4bef4cd7..f92bf933 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterUsageListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterUsageListingActivity.java @@ -4,6 +4,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.content.res.ColorStateList; import android.os.Bundle; +import android.os.Handler; import android.view.MenuItem; import android.view.View; import android.view.inputmethod.InputMethodManager; @@ -40,6 +41,7 @@ import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.postfilter.PostFilterUsage; import ml.docilealligator.infinityforreddit.postfilter.PostFilterUsageViewModel; import ml.docilealligator.infinityforreddit.postfilter.SavePostFilterUsage; +import ml.docilealligator.infinityforreddit.utils.Utils; public class PostFilterUsageListingActivity extends BaseActivity { @@ -151,18 +153,13 @@ public class PostFilterUsageListingActivity extends BaseActivity { break; } - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), textInputEditText); new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) .setTitle(titleStringId) .setView(dialogView) .setPositiveButton(R.string.ok, (editTextDialogInterface, i1) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(textInputEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); PostFilterUsage postFilterUsage; if (textInputEditText.getText().toString().equals("")) { @@ -175,9 +172,7 @@ public class PostFilterUsageListingActivity extends BaseActivity { }) .setNegativeButton(R.string.cancel, null) .setOnDismissListener(editTextDialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(textInputEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .show(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java index f9fa34f3..788567fc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java @@ -7,6 +7,7 @@ import android.content.SharedPreferences; import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.os.Handler; import android.text.Editable; import android.text.TextWatcher; import android.view.KeyEvent; @@ -411,19 +412,13 @@ public class SearchActivity extends BaseActivity { query = null; } - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), searchEditText); } @Override protected void onPause() { super.onPause(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java index f20299fa..670411b8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java @@ -1,10 +1,10 @@ package ml.docilealligator.infinityforreddit.activities; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; +import android.os.Handler; import android.text.Editable; import android.text.TextWatcher; import android.view.KeyEvent; @@ -14,7 +14,6 @@ import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.inputmethod.EditorInfo; -import android.view.inputmethod.InputMethodManager; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -583,25 +582,18 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect TextInputEditText thingEditText = rootView.findViewById(R.id.text_input_edit_text_go_to_thing_edit_text); RecyclerView recyclerView = rootView.findViewById(R.id.recycler_view_go_to_thing_edit_text); thingEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); SubredditAutocompleteRecyclerViewAdapter adapter = new SubredditAutocompleteRecyclerViewAdapter( this, mCustomThemeWrapper, subredditData -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent intent = new Intent(SearchResultActivity.this, ViewSubredditDetailActivity.class); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subredditData.getName()); startActivity(intent); }); recyclerView.setAdapter(adapter); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), thingEditText); thingEditText.setOnEditorActionListener((textView, i, keyEvent) -> { if (i == EditorInfo.IME_ACTION_DONE) { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent subredditIntent = new Intent(this, ViewSubredditDetailActivity.class); subredditIntent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, thingEditText.getText().toString()); startActivity(subredditIntent); @@ -659,22 +651,16 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect .setView(rootView) .setPositiveButton(R.string.ok, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent subredditIntent = new Intent(this, ViewSubredditDetailActivity.class); subredditIntent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, thingEditText.getText().toString()); startActivity(subredditIntent); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .setOnDismissListener(dialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .show(); } @@ -683,15 +669,10 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect View rootView = getLayoutInflater().inflate(R.layout.dialog_go_to_thing_edit_text, coordinatorLayout, false); TextInputEditText thingEditText = rootView.findViewById(R.id.text_input_edit_text_go_to_thing_edit_text); thingEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), thingEditText); thingEditText.setOnEditorActionListener((textView, i, keyEvent) -> { if (i == EditorInfo.IME_ACTION_DONE) { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent userIntent = new Intent(this, ViewUserDetailActivity.class); userIntent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, thingEditText.getText().toString()); startActivity(userIntent); @@ -704,22 +685,16 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect .setView(rootView) .setPositiveButton(R.string.ok, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent userIntent = new Intent(this, ViewUserDetailActivity.class); userIntent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, thingEditText.getText().toString()); startActivity(userIntent); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .setOnDismissListener(dialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .show(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SelectUserFlairActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SelectUserFlairActivity.java index 9f875d70..5287c47c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SelectUserFlairActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SelectUserFlairActivity.java @@ -1,12 +1,11 @@ package ml.docilealligator.infinityforreddit.activities; -import android.content.Context; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; +import android.os.Handler; import android.view.MenuItem; import android.view.View; -import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.Toast; @@ -38,6 +37,7 @@ import ml.docilealligator.infinityforreddit.adapters.UserFlairRecyclerViewAdapte import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; +import ml.docilealligator.infinityforreddit.utils.Utils; import retrofit2.Retrofit; public class SelectUserFlairActivity extends BaseActivity implements ActivityToolbarInterface { @@ -136,30 +136,21 @@ public class SelectUserFlairActivity extends BaseActivity implements ActivityToo EditText flairEditText = dialogView.findViewById(R.id.flair_edit_text_edit_flair_dialog); flairEditText.setText(userFlair.getText()); flairEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), flairEditText); new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) .setTitle(R.string.edit_flair) .setView(dialogView) .setPositiveButton(R.string.ok, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(flairEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); userFlair.setText(flairEditText.getText().toString()); selectUserFlair(userFlair); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(flairEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .setOnDismissListener(dialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(flairEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .show(); } else { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java index 72e249a7..1faf7022 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java @@ -275,11 +275,7 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti return true; } else if (item.getItemId() == android.R.id.home) { if (searchEditText.getVisibility() == View.VISIBLE) { - View view = this.getCurrentFocus(); - if (view != null) { - InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); - imm.hideSoftInputFromWindow(view.getWindowToken(), 0); - } + Utils.hideKeyboard(this); searchEditText.setVisibility(View.GONE); searchEditText.setText(""); mMenu.findItem(R.id.action_search_subscribed_thing_listing_activity).setVisible(true); @@ -296,11 +292,7 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti @Override public void onBackPressed() { if (searchEditText.getVisibility() == View.VISIBLE) { - View view = this.getCurrentFocus(); - if (view != null) { - InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); - imm.hideSoftInputFromWindow(view.getWindowToken(), 0); - } + Utils.hideKeyboard(this); searchEditText.setVisibility(View.GONE); searchEditText.setText(""); mMenu.findItem(R.id.action_search_subscribed_thing_listing_activity).setVisible(true); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java index b59e9f27..01264ace 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java @@ -1,6 +1,5 @@ package ml.docilealligator.infinityforreddit.activities; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; @@ -16,7 +15,6 @@ import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.inputmethod.EditorInfo; -import android.view.inputmethod.InputMethodManager; import android.widget.Toast; import androidx.annotation.NonNull; @@ -607,25 +605,18 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT TextInputEditText thingEditText = rootView.findViewById(R.id.text_input_edit_text_go_to_thing_edit_text); RecyclerView recyclerView = rootView.findViewById(R.id.recycler_view_go_to_thing_edit_text); thingEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); SubredditAutocompleteRecyclerViewAdapter adapter = new SubredditAutocompleteRecyclerViewAdapter( this, mCustomThemeWrapper, subredditData -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent intent = new Intent(this, ViewSubredditDetailActivity.class); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subredditData.getName()); startActivity(intent); }); recyclerView.setAdapter(adapter); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), thingEditText); thingEditText.setOnEditorActionListener((textView, i, keyEvent) -> { if (i == EditorInfo.IME_ACTION_DONE) { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent subredditIntent = new Intent(this, ViewSubredditDetailActivity.class); subredditIntent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, thingEditText.getText().toString()); startActivity(subredditIntent); @@ -683,22 +674,16 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT .setView(rootView) .setPositiveButton(R.string.ok, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent subredditIntent = new Intent(this, ViewSubredditDetailActivity.class); subredditIntent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, thingEditText.getText().toString()); startActivity(subredditIntent); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .setOnDismissListener(dialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .show(); } @@ -707,15 +692,10 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT View rootView = getLayoutInflater().inflate(R.layout.dialog_go_to_thing_edit_text, coordinatorLayout, false); TextInputEditText thingEditText = rootView.findViewById(R.id.text_input_edit_text_go_to_thing_edit_text); thingEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), thingEditText); thingEditText.setOnEditorActionListener((textView, i, keyEvent) -> { if (i == EditorInfo.IME_ACTION_DONE) { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent userIntent = new Intent(this, ViewUserDetailActivity.class); userIntent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, thingEditText.getText().toString()); startActivity(userIntent); @@ -728,22 +708,16 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT .setView(rootView) .setPositiveButton(R.string.ok, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent userIntent = new Intent(this, ViewUserDetailActivity.class); userIntent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, thingEditText.getText().toString()); startActivity(userIntent); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .setOnDismissListener(dialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .show(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java index 0cf5bbf3..e531a84b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java @@ -1403,25 +1403,18 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp TextInputEditText thingEditText = rootView.findViewById(R.id.text_input_edit_text_go_to_thing_edit_text); RecyclerView recyclerView = rootView.findViewById(R.id.recycler_view_go_to_thing_edit_text); thingEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); SubredditAutocompleteRecyclerViewAdapter adapter = new SubredditAutocompleteRecyclerViewAdapter( this, mCustomThemeWrapper, subredditData -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent intent = new Intent(ViewSubredditDetailActivity.this, ViewSubredditDetailActivity.class); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subredditData.getName()); startActivity(intent); }); recyclerView.setAdapter(adapter); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), thingEditText); thingEditText.setOnEditorActionListener((textView, i, keyEvent) -> { if (i == EditorInfo.IME_ACTION_DONE) { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent subredditIntent = new Intent(this, ViewSubredditDetailActivity.class); subredditIntent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, thingEditText.getText().toString()); startActivity(subredditIntent); @@ -1479,22 +1472,16 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp .setView(rootView) .setPositiveButton(R.string.ok, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent subredditIntent = new Intent(this, ViewSubredditDetailActivity.class); subredditIntent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, thingEditText.getText().toString()); startActivity(subredditIntent); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .setOnDismissListener(dialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .show(); } @@ -1503,15 +1490,10 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp View rootView = getLayoutInflater().inflate(R.layout.dialog_go_to_thing_edit_text, coordinatorLayout, false); TextInputEditText thingEditText = rootView.findViewById(R.id.text_input_edit_text_go_to_thing_edit_text); thingEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), thingEditText); thingEditText.setOnEditorActionListener((textView, i, keyEvent) -> { if (i == EditorInfo.IME_ACTION_DONE) { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent userIntent = new Intent(this, ViewUserDetailActivity.class); userIntent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, thingEditText.getText().toString()); startActivity(userIntent); @@ -1524,22 +1506,16 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp .setView(rootView) .setPositiveButton(R.string.ok, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent userIntent = new Intent(this, ViewUserDetailActivity.class); userIntent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, thingEditText.getText().toString()); startActivity(userIntent); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .setOnDismissListener(dialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .show(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java index 72ad5960..b1119059 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -1346,25 +1346,18 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele TextInputEditText thingEditText = rootView.findViewById(R.id.text_input_edit_text_go_to_thing_edit_text); RecyclerView recyclerView = rootView.findViewById(R.id.recycler_view_go_to_thing_edit_text); thingEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); SubredditAutocompleteRecyclerViewAdapter adapter = new SubredditAutocompleteRecyclerViewAdapter( this, mCustomThemeWrapper, subredditData -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent intent = new Intent(ViewUserDetailActivity.this, ViewSubredditDetailActivity.class); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subredditData.getName()); startActivity(intent); }); recyclerView.setAdapter(adapter); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), thingEditText); thingEditText.setOnEditorActionListener((textView, i, keyEvent) -> { if (i == EditorInfo.IME_ACTION_DONE) { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent subredditIntent = new Intent(this, ViewSubredditDetailActivity.class); subredditIntent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, thingEditText.getText().toString()); startActivity(subredditIntent); @@ -1422,22 +1415,16 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele .setView(rootView) .setPositiveButton(R.string.ok, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent subredditIntent = new Intent(this, ViewSubredditDetailActivity.class); subredditIntent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, thingEditText.getText().toString()); startActivity(subredditIntent); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .setOnDismissListener(dialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .show(); } @@ -1446,15 +1433,10 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele View rootView = getLayoutInflater().inflate(R.layout.dialog_go_to_thing_edit_text, coordinatorLayout, false); TextInputEditText thingEditText = rootView.findViewById(R.id.text_input_edit_text_go_to_thing_edit_text); thingEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(this, new Handler(), thingEditText); thingEditText.setOnEditorActionListener((textView, i, keyEvent) -> { if (i == EditorInfo.IME_ACTION_DONE) { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent userIntent = new Intent(this, ViewUserDetailActivity.class); userIntent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, thingEditText.getText().toString()); startActivity(userIntent); @@ -1467,22 +1449,16 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele .setView(rootView) .setPositiveButton(R.string.ok, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); Intent userIntent = new Intent(this, ViewUserDetailActivity.class); userIntent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, thingEditText.getText().toString()); startActivity(userIntent); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .setOnDismissListener(dialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(this); }) .show(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CustomizeThemeRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CustomizeThemeRecyclerViewAdapter.java index eb4fddb5..02990b84 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CustomizeThemeRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CustomizeThemeRecyclerViewAdapter.java @@ -1,11 +1,10 @@ package ml.docilealligator.infinityforreddit.adapters; -import android.content.Context; import android.content.res.ColorStateList; +import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.TextView; @@ -24,6 +23,7 @@ import ml.docilealligator.infinityforreddit.activities.BaseActivity; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeSettingsItem; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.ColorPickerDialog; +import ml.docilealligator.infinityforreddit.utils.Utils; public class CustomizeThemeRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private static final int VIEW_TYPE_COLOR = 1; @@ -92,30 +92,21 @@ public class CustomizeThemeRecyclerViewAdapter extends RecyclerView.Adapter<Recy EditText themeNameEditText = dialogView.findViewById(R.id.name_edit_text_edit_name_dialog); themeNameEditText.setText(themeName); themeNameEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(activity, new Handler(), themeNameEditText); new MaterialAlertDialogBuilder(activity, R.style.MaterialAlertDialogTheme) .setTitle(R.string.edit_theme_name) .setView(dialogView) .setPositiveButton(R.string.ok, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(themeNameEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(activity); themeName = themeNameEditText.getText().toString(); ((ThemeNameItemViewHolder) holder).themeNameTextView.setText(themeName); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(themeNameEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(activity); }) .setOnDismissListener(dialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(themeNameEditText.getWindowToken(), 0); - } + Utils.hideKeyboard(activity); }) .show(); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/FlairBottomSheetRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/FlairBottomSheetRecyclerViewAdapter.java index cc36dde5..ffaedc2a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/FlairBottomSheetRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/FlairBottomSheetRecyclerViewAdapter.java @@ -1,11 +1,9 @@ package ml.docilealligator.infinityforreddit.adapters; -import android.app.Activity; -import android.content.Context; +import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; @@ -23,6 +21,7 @@ import ml.docilealligator.infinityforreddit.Flair; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.activities.BaseActivity; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.utils.Utils; public class FlairBottomSheetRecyclerViewAdapter extends RecyclerView.Adapter<FlairBottomSheetRecyclerViewAdapter.FlairViewHolder> { private BaseActivity activity; @@ -48,13 +47,10 @@ public class FlairBottomSheetRecyclerViewAdapter extends RecyclerView.Adapter<Fl if (flairs.get(holder.getBindingAdapterPosition()).isEditable()) { holder.editFlairImageView.setVisibility(View.VISIBLE); holder.editFlairImageView.setOnClickListener(view -> { - View dialogView = ((Activity) activity).getLayoutInflater().inflate(R.layout.dialog_edit_flair, null); + View dialogView = activity.getLayoutInflater().inflate(R.layout.dialog_edit_flair, null); EditText flairEditText = dialogView.findViewById(R.id.flair_edit_text_edit_flair_dialog); flairEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(activity, new Handler(), flairEditText); new MaterialAlertDialogBuilder(activity, R.style.MaterialAlertDialogTheme) .setTitle(R.string.edit_flair) .setView(dialogView) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostGalleryTypeImageRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostGalleryTypeImageRecyclerViewAdapter.java index e3bdae8a..41b00634 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostGalleryTypeImageRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostGalleryTypeImageRecyclerViewAdapter.java @@ -5,7 +5,6 @@ import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.net.Uri; import android.text.TextUtils; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -84,7 +83,6 @@ public class PostGalleryTypeImageRecyclerViewAdapter extends RecyclerView.Adapte public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) { if (ratio < 0) { int height = (int) (400 * mScale); - Log.i("asdfasdf", "asdfadsf"); holder.binding.imageViewItemGalleryImageInPostFeed.setScaleType(ImageView.ScaleType.CENTER_CROP); holder.binding.imageViewItemGalleryImageInPostFeed.getLayoutParams().height = height; } else { @@ -104,8 +102,6 @@ public class PostGalleryTypeImageRecyclerViewAdapter extends RecyclerView.Adapte if (showCaption) { loadCaptionPreview(holder); } - - loadImage(holder); } @Override @@ -122,6 +118,10 @@ public class PostGalleryTypeImageRecyclerViewAdapter extends RecyclerView.Adapte } private void loadImage(ImageViewHolder holder) { + if (galleryImages == null) { + return; + } + RequestBuilder<Drawable> imageRequestBuilder = glide.load(galleryImages.get(holder.getBindingAdapterPosition()).url).listener(new RequestListener<>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/CustomizeMainPageTabsFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/CustomizeMainPageTabsFragment.java index 4346fef7..6d971102 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/CustomizeMainPageTabsFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/CustomizeMainPageTabsFragment.java @@ -6,6 +6,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.graphics.drawable.Drawable; import android.os.Bundle; +import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -238,10 +239,7 @@ public class CustomizeMainPageTabsFragment extends Fragment { editText.setHint(R.string.settings_tab_title); editText.setText(tab1CurrentTitle); editText.requestFocus(); - InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(activity, new Handler(), editText); if (dialogView.getParent() != null) { ((ViewGroup) dialogView.getParent()).removeView(dialogView); } @@ -253,14 +251,10 @@ public class CustomizeMainPageTabsFragment extends Fragment { tab1CurrentTitle = editText.getText().toString(); mainActivityTabsSharedPreferences.edit().putString((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_1_TITLE, tab1CurrentTitle).apply(); tab1TitleSummaryTextView.setText(tab1CurrentTitle); - if (imm != null) { - imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); - } + Utils.hideKeyboard(activity); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); - } + Utils.hideKeyboard(activity); }) .show(); }); @@ -297,10 +291,7 @@ public class CustomizeMainPageTabsFragment extends Fragment { editText.setText(tab1CurrentName); editText.setHint(titleId); editText.requestFocus(); - InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(activity, new Handler(), editText); if (dialogView.getParent() != null) { ((ViewGroup) dialogView.getParent()).removeView(dialogView); } @@ -312,14 +303,10 @@ public class CustomizeMainPageTabsFragment extends Fragment { tab1CurrentName = editText.getText().toString(); mainActivityTabsSharedPreferences.edit().putString((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_1_NAME, tab1CurrentName).apply(); tab1NameSummaryTextView.setText(tab1CurrentName); - if (imm != null) { - imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); - } + Utils.hideKeyboard(activity); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); - } + Utils.hideKeyboard(activity); }) .show(); }); @@ -338,10 +325,7 @@ public class CustomizeMainPageTabsFragment extends Fragment { editText.setHint(R.string.settings_tab_title); editText.setText(tab2CurrentTitle); editText.requestFocus(); - InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(activity, new Handler(), editText); if (dialogView.getParent() != null) { ((ViewGroup) dialogView.getParent()).removeView(dialogView); } @@ -353,14 +337,10 @@ public class CustomizeMainPageTabsFragment extends Fragment { tab2CurrentTitle = editText.getText().toString(); mainActivityTabsSharedPreferences.edit().putString((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_2_TITLE, tab2CurrentTitle).apply(); tab2TitleSummaryTextView.setText(tab2CurrentTitle); - if (imm != null) { - imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); - } + Utils.hideKeyboard(activity); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); - } + Utils.hideKeyboard(activity); }) .show(); }); @@ -397,10 +377,7 @@ public class CustomizeMainPageTabsFragment extends Fragment { editText.setText(tab2CurrentName); editText.setHint(titleId); editText.requestFocus(); - InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(activity, new Handler(), editText); if (dialogView.getParent() != null) { ((ViewGroup) dialogView.getParent()).removeView(dialogView); } @@ -412,14 +389,10 @@ public class CustomizeMainPageTabsFragment extends Fragment { tab2CurrentName = editText.getText().toString(); mainActivityTabsSharedPreferences.edit().putString((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_2_NAME, tab2CurrentName).apply(); tab2NameSummaryTextView.setText(tab2CurrentName); - if (imm != null) { - imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); - } + Utils.hideKeyboard(activity); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); - } + Utils.hideKeyboard(activity); }) .show(); }); @@ -438,10 +411,7 @@ public class CustomizeMainPageTabsFragment extends Fragment { editText.setHint(R.string.settings_tab_title); editText.setText(tab3CurrentTitle); editText.requestFocus(); - InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(activity, new Handler(), editText); if (dialogView.getParent() != null) { ((ViewGroup) dialogView.getParent()).removeView(dialogView); } @@ -453,14 +423,10 @@ public class CustomizeMainPageTabsFragment extends Fragment { tab3CurrentTitle = editText.getText().toString(); mainActivityTabsSharedPreferences.edit().putString((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_3_TITLE, tab3CurrentTitle).apply(); tab3TitleSummaryTextView.setText(tab3CurrentTitle); - if (imm != null) { - imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); - } + Utils.hideKeyboard(activity); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); - } + Utils.hideKeyboard(activity); }) .show(); }); @@ -497,10 +463,7 @@ public class CustomizeMainPageTabsFragment extends Fragment { editText.setText(tab3CurrentName); editText.setHint(titleId); editText.requestFocus(); - InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } + Utils.showKeyboard(activity, new Handler(), editText); if (dialogView.getParent() != null) { ((ViewGroup) dialogView.getParent()).removeView(dialogView); } @@ -512,14 +475,10 @@ public class CustomizeMainPageTabsFragment extends Fragment { tab3CurrentName = editText.getText().toString(); mainActivityTabsSharedPreferences.edit().putString((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_3_NAME, tab3CurrentName).apply(); tab3NameSummaryTextView.setText(tab3CurrentName); - if (imm != null) { - imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); - } + Utils.hideKeyboard(activity); }) .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); - } + Utils.hideKeyboard(activity); }) .show(); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java index fc3fc620..40bc8cd1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java @@ -332,6 +332,15 @@ public final class Utils { } } + public static void showKeyboard(Context context, Handler handler, View view) { + handler.postDelayed(() -> { + InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); + if (imm != null) { + imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); + } + }, 300); + } + public static void hideKeyboard(Activity activity) { InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); if (inputMethodManager != null && activity.getCurrentFocus() != null) { |