From 7426b39b011cacbf85a49c46d0c953f23f5cf32f Mon Sep 17 00:00:00 2001 From: Darshan Parajuli Date: Sat, 21 Mar 2020 15:52:46 -0700 Subject: [PATCH] Code cleanup --- .../fragments/filters/FiltersFragment.kt | 7 ++-- .../filters/dialogs/FilterDialogFragment.kt | 2 +- .../logcatlive/LogcatLiveFragment.kt | 39 ++++++++++--------- .../AskingForRootAccessDialogFragment.kt | 2 +- ...alMethodToGrantPermissionDialogFragment.kt | 7 ++-- .../dialogs/NeedPermissionDialogFragment.kt | 2 +- .../OnSavedBottomSheetDialogFragment.kt | 5 ++- .../RestartAppMessageDialogFragment.kt | 5 ++- .../fragments/savedlogs/SavedLogsFragment.kt | 13 ++++--- .../SavedLogsViewerFragment.kt | 4 +- .../fragments/settings/SettingsFragment.kt | 26 +++++++------ .../dialogs/FolderChooserDialogFragment.kt | 7 ++-- .../dialogs/CopyToClipboardDialogFragment.kt | 9 +++-- .../dialogs/FilterExclusionDialogFragment.kt | 11 +++--- 14 files changed, 76 insertions(+), 63 deletions(-) diff --git a/app/src/main/java/com/dp/logcatapp/fragments/filters/FiltersFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/filters/FiltersFragment.kt index 52cb55a8..df7c32c5 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/filters/FiltersFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/filters/FiltersFragment.kt @@ -44,7 +44,7 @@ class FiltersFragment : BaseFragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setHasOptionsMenu(true) - viewModel = activity!!.getAndroidViewModel() + viewModel = requireActivity().getAndroidViewModel() recyclerViewAdapter = MyRecyclerViewAdapter { onRemoveClicked(it) } @@ -88,10 +88,11 @@ class FiltersFragment : BaseFragment() { emptyMessage = rootView.findViewById(R.id.textViewEmpty) + val activity = requireActivity() val recyclerView = rootView.findViewById(R.id.recyclerView) - recyclerView.addItemDecoration(DividerItemDecoration(activity!!, + recyclerView.addItemDecoration(DividerItemDecoration(activity, DividerItemDecoration.VERTICAL)) - linearLayoutManager = LinearLayoutManager(activity!!) + linearLayoutManager = LinearLayoutManager(activity) recyclerView.layoutManager = linearLayoutManager recyclerView.adapter = recyclerViewAdapter diff --git a/app/src/main/java/com/dp/logcatapp/fragments/filters/dialogs/FilterDialogFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/filters/dialogs/FilterDialogFragment.kt index c3b524f9..1cc2c4df 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/filters/dialogs/FilterDialogFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/filters/dialogs/FilterDialogFragment.kt @@ -127,7 +127,7 @@ class FilterDialogFragment : BaseDialogFragment() { getString(R.string.filter) } - return AlertDialog.Builder(activity!!) + return AlertDialog.Builder(requireActivity()) .setTitle(title) .setView(rootView) .setPositiveButton(android.R.string.ok) { _, _ -> diff --git a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/LogcatLiveFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/LogcatLiveFragment.kt index 350dd232..267d2cfe 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/LogcatLiveFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/LogcatLiveFragment.kt @@ -182,13 +182,14 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen setHasOptionsMenu(true) serviceBinder = ServiceBinder(LogcatService::class.java, this) - val maxLogs = activity!!.getDefaultSharedPreferences() + val activity = requireActivity() + val maxLogs = activity.getDefaultSharedPreferences() .getString(PreferenceKeys.Logcat.KEY_MAX_LOGS, PreferenceKeys.Logcat.Default.MAX_LOGS)!!.trim().toInt() - adapter = MyRecyclerViewAdapter(activity!!, maxLogs) - activity!!.getDefaultSharedPreferences().registerOnSharedPreferenceChangeListener(adapter) + adapter = MyRecyclerViewAdapter(activity, maxLogs) + activity.getDefaultSharedPreferences().registerOnSharedPreferenceChangeListener(adapter) - viewModel = activity!!.getAndroidViewModel() + viewModel = activity.getAndroidViewModel() } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, @@ -318,9 +319,8 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen }) } - private fun checkReadLogsPermission() = - ContextCompat.checkSelfPermission(activity!!, - Manifest.permission.READ_LOGS) == PackageManager.PERMISSION_GRANTED + private fun checkReadLogsPermission() = ContextCompat.checkSelfPermission(requireContext(), + Manifest.permission.READ_LOGS) == PackageManager.PERMISSION_GRANTED override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { super.onCreateOptionsMenu(menu, inflater) @@ -404,23 +404,24 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen val playPauseItem = menu.findItem(R.id.action_play_pause) val recordToggleItem = menu.findItem(R.id.action_record_toggle) + val context = requireContext() logcatService?.let { if (it.paused) { - playPauseItem.icon = ContextCompat.getDrawable(activity!!, + playPauseItem.icon = ContextCompat.getDrawable(context, R.drawable.ic_play_arrow_white_24dp) playPauseItem.title = getString(R.string.resume) } else { - playPauseItem.icon = ContextCompat.getDrawable(activity!!, + playPauseItem.icon = ContextCompat.getDrawable(context, R.drawable.ic_pause_white_24dp) playPauseItem.title = getString(R.string.pause) } if (it.recording) { - recordToggleItem.icon = ContextCompat.getDrawable(activity!!, + recordToggleItem.icon = ContextCompat.getDrawable(context, R.drawable.ic_stop_white_24dp) recordToggleItem.title = getString(R.string.stop_recording) } else { - recordToggleItem.icon = ContextCompat.getDrawable(activity!!, + recordToggleItem.icon = ContextCompat.getDrawable(context, R.drawable.ic_fiber_manual_record_white_24dp) recordToggleItem.title = getString(R.string.start_recording) } @@ -458,9 +459,8 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen val logcat = it.logcat if (recording) { - Snackbar.make(view!!, getString(R.string.started_recording), - Snackbar.LENGTH_SHORT) - .show() + Snackbar.make(requireView(), getString(R.string.started_recording), + Snackbar.LENGTH_SHORT).show() logcat.startRecording() } else { saveToFile(true) @@ -504,7 +504,7 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen } private fun moveToFilterActivity(isExclusion: Boolean) { - val intent = Intent(activity!!, FiltersActivity::class.java) + val intent = Intent(requireActivity(), FiltersActivity::class.java) intent.putExtra(FiltersActivity.EXTRA_EXCLUSIONS, isExclusion) startActivity(intent) } @@ -541,12 +541,12 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen override fun onStart() { super.onStart() - serviceBinder.bind(activity!!) + serviceBinder.bind(requireActivity()) } override fun onStop() { super.onStop() - serviceBinder.unbind(activity!!) + serviceBinder.unbind(requireActivity()) } private fun removeLastSearchRunnableCallback() { @@ -558,7 +558,8 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen override fun onDestroy() { super.onDestroy() - activity!!.getDefaultSharedPreferences().unregisterOnSharedPreferenceChangeListener(adapter) + requireContext().getDefaultSharedPreferences() + .unregisterOnSharedPreferenceChangeListener(adapter) removeLastSearchRunnableCallback() searchTask?.cancel() @@ -679,7 +680,7 @@ class LogcatLiveFragment : BaseFragment(), ServiceConnection, LogsReceivedListen RestartAppMessageDialogFragment.newInstance().show(parentFragmentManager, RestartAppMessageDialogFragment.TAG) } else { - activity!!.showToast(getString(R.string.fail)) + requireActivity().showToast(getString(R.string.fail)) ManualMethodToGrantPermissionDialogFragment().show(parentFragmentManager, ManualMethodToGrantPermissionDialogFragment.TAG) } diff --git a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/AskingForRootAccessDialogFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/AskingForRootAccessDialogFragment.kt index 006cb028..27e163e0 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/AskingForRootAccessDialogFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/AskingForRootAccessDialogFragment.kt @@ -13,7 +13,7 @@ class AskingForRootAccessDialogFragment : BaseDialogFragment() { val view = inflateLayout(R.layout.asking_for_root_access_dialog_fragment) isCancelable = false - return AlertDialog.Builder(activity!!) + return AlertDialog.Builder(requireActivity()) .setView(view) .setCancelable(false) .create() diff --git a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/ManualMethodToGrantPermissionDialogFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/ManualMethodToGrantPermissionDialogFragment.kt index 1c2858f3..63f661a4 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/ManualMethodToGrantPermissionDialogFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/ManualMethodToGrantPermissionDialogFragment.kt @@ -15,13 +15,14 @@ class ManualMethodToGrantPermissionDialogFragment : BaseDialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val view = inflateLayout(R.layout.manual_method_dialog_fragment) - return AlertDialog.Builder(activity!!) + return AlertDialog.Builder(requireActivity()) .setTitle(getString(R.string.manual_method)) .setView(view) .setPositiveButton(R.string.copy_adb_command) { _, _ -> - val cmd = "adb shell pm grant ${activity!!.packageName} " + + val activity = requireActivity() + val cmd = "adb shell pm grant ${activity.packageName} " + Manifest.permission.READ_LOGS - val cm = activity!!.getSystemService(Context.CLIPBOARD_SERVICE) + val cm = activity.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager cm.setPrimaryClip(ClipData.newPlainText("Adb command", cmd)) } diff --git a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/NeedPermissionDialogFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/NeedPermissionDialogFragment.kt index fa2ce2df..5004ada8 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/NeedPermissionDialogFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/NeedPermissionDialogFragment.kt @@ -10,7 +10,7 @@ import com.dp.logcatapp.fragments.logcatlive.LogcatLiveFragment class NeedPermissionDialogFragment : BaseDialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - return AlertDialog.Builder(activity!!) + return AlertDialog.Builder(requireActivity()) .setTitle(R.string.read_logs_permission_required) .setMessage(R.string.read_logs_permission_required_msg) .setPositiveButton(R.string.manual_method) { _, _ -> diff --git a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/OnSavedBottomSheetDialogFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/OnSavedBottomSheetDialogFragment.kt index f9179e82..0deda6fc 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/OnSavedBottomSheetDialogFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/OnSavedBottomSheetDialogFragment.kt @@ -35,7 +35,7 @@ class OnSavedBottomSheetDialogFragment : BottomSheetDialogFragment() { savedInstanceState: Bundle?): View { val rootView = inflater.inflate(R.layout.saved_log_bottom_sheet, container, false) - val arguments = arguments!! + val arguments = requireArguments() val fileName = arguments.getString(KEY_FILE_NAME) val uri = Uri.parse(arguments.getString(KEY_URI)) @@ -48,7 +48,8 @@ class OnSavedBottomSheetDialogFragment : BottomSheetDialogFragment() { } rootView.findViewById(R.id.actionShare).setOnClickListener { - ShareUtils.shareSavedLogs(context!!, uri, Utils.isUsingCustomSaveLocation(context!!)) + val context = requireContext() + ShareUtils.shareSavedLogs(context, uri, Utils.isUsingCustomSaveLocation(context)) dismiss() } diff --git a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/RestartAppMessageDialogFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/RestartAppMessageDialogFragment.kt index cbc2d945..f8c38aae 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/RestartAppMessageDialogFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/logcatlive/dialogs/RestartAppMessageDialogFragment.kt @@ -21,12 +21,13 @@ class RestartAppMessageDialogFragment : BaseDialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { isCancelable = false - return AlertDialog.Builder(activity!!) + return AlertDialog.Builder(requireActivity()) .setTitle(R.string.app_restart_dialog_title) .setMessage(getString(R.string.app_restart_dialog_msg_body)) .setCancelable(false) .setPositiveButton(android.R.string.ok) { _, _ -> - context!!.stopService(Intent(context, LogcatService::class.java)) + val context = requireContext() + context.stopService(Intent(context, LogcatService::class.java)) Process.killProcess(Process.myPid()) } .create() diff --git a/app/src/main/java/com/dp/logcatapp/fragments/savedlogs/SavedLogsFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/savedlogs/SavedLogsFragment.kt index 215a3bca..a9750ca6 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/savedlogs/SavedLogsFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/savedlogs/SavedLogsFragment.kt @@ -63,8 +63,8 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick super.onCreate(savedInstanceState) viewModel = getAndroidViewModel() - recyclerViewAdapter = MyRecyclerViewAdapter(activity!!, this, - this, viewModel.selectedItems) + recyclerViewAdapter = MyRecyclerViewAdapter(requireActivity(), + this, this, viewModel.selectedItems) } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, @@ -202,7 +202,7 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick } R.id.action_share -> { val fileInfo = recyclerViewAdapter.getItem(viewModel.selectedItems.toIntArray()[0]) - ShareUtils.shareSavedLogs(context!!, Uri.parse(fileInfo.info.path), + ShareUtils.shareSavedLogs(requireContext(), Uri.parse(fileInfo.info.path), fileInfo.info.isCustom) true } @@ -264,14 +264,15 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick } private fun saveToDeviceFallback() { + val activity = requireActivity() if (Environment.getExternalStorageState() != Environment.MEDIA_MOUNTED) { - activity!!.showToast(getString(R.string.external_storage_not_mounted_error)) + activity.showToast(getString(R.string.external_storage_not_mounted_error)) return } val fileInfo = recyclerViewAdapter.getItem(viewModel.selectedItems.toIntArray()[0]) val fileName = fileInfo.info.fileName - val srcFolder = File(context!!.filesDir, LogcatLiveFragment.LOGCAT_DIR) + val srcFolder = File(activity.filesDir, LogcatLiveFragment.LOGCAT_DIR) val src = File(srcFolder, fileName) @Suppress("DEPRECATION") @@ -280,7 +281,7 @@ class SavedLogsFragment : BaseFragment(), View.OnClickListener, View.OnLongClick val destFolder = File(documentsFolder, "LogcatReader") if (!destFolder.exists()) { if (!destFolder.mkdirs()) { - activity!!.showToast(getString(R.string.error_saving)) + activity.showToast(getString(R.string.error_saving)) return } } diff --git a/app/src/main/java/com/dp/logcatapp/fragments/savedlogsviewer/SavedLogsViewerFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/savedlogsviewer/SavedLogsViewerFragment.kt index 10212a6d..70d043bf 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/savedlogsviewer/SavedLogsViewerFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/savedlogsviewer/SavedLogsViewerFragment.kt @@ -157,9 +157,9 @@ class SavedLogsViewerFragment : BaseFragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setHasOptionsMenu(true) - adapter = MyRecyclerViewAdapter(activity!!) + adapter = MyRecyclerViewAdapter(requireActivity()) viewModel = getAndroidViewModel() - viewModel.init(Uri.parse(arguments!!.getString(KEY_FILE_URI))) + viewModel.init(Uri.parse(requireArguments().getString(KEY_FILE_URI))) } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, diff --git a/app/src/main/java/com/dp/logcatapp/fragments/settings/SettingsFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/settings/SettingsFragment.kt index 6db0e04e..c1ccf495 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/settings/SettingsFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/settings/SettingsFragment.kt @@ -66,14 +66,15 @@ class SettingsFragment : PreferenceFragmentCompat() { themePref.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { preference, newValue -> preference.summary = themePrefEntries[(newValue as String).toInt()] - activity!!.restartApp() + requireActivity().restartApp() true } useBlackThemePref.onPreferenceChangeListener = Preference .OnPreferenceChangeListener { _, _ -> - if (activity!!.isDarkThemeOn()) { - activity!!.restartApp() + val activity = requireActivity() + if (activity.isDarkThemeOn()) { + activity.restartApp() } true } @@ -90,18 +91,19 @@ class SettingsFragment : PreferenceFragmentCompat() { prefPollInterval.onPreferenceChangeListener = Preference .OnPreferenceChangeListener { preference, newValue -> + val activity = requireActivity() try { val v = newValue.toString().trim() val num = v.toLong() if (num <= 0) { - activity!!.showToast(getString(R.string.value_must_be_greater_than_0)) + activity.showToast(getString(R.string.value_must_be_greater_than_0)) false } else { preference.summary = "$v ms" true } } catch (e: NumberFormatException) { - activity!!.showToast(getString(R.string.value_must_be_a_positive_integer)) + activity.showToast(getString(R.string.value_must_be_a_positive_integer)) false } } @@ -151,6 +153,7 @@ class SettingsFragment : PreferenceFragmentCompat() { prefMaxLogs.summary = NumberFormat.getInstance().format(maxLogs) prefMaxLogs.onPreferenceChangeListener = Preference .OnPreferenceChangeListener callback@{ preference, newValue -> + val activity = requireActivity() try { val oldValue = preferenceScreen.sharedPreferences.getString( PreferenceKeys.Logcat.KEY_MAX_LOGS, @@ -163,14 +166,14 @@ class SettingsFragment : PreferenceFragmentCompat() { } if (newMaxLogs < 1000) { - activity!!.showToast(getString(R.string.cannot_be_less_than_1000)) + activity.showToast(getString(R.string.cannot_be_less_than_1000)) return@callback false } preference.summary = NumberFormat.getInstance().format(newMaxLogs) true } catch (e: NumberFormatException) { - activity!!.showToast(getString(R.string.not_a_valid_number)) + activity.showToast(getString(R.string.not_a_valid_number)) false } } @@ -222,17 +225,18 @@ class SettingsFragment : PreferenceFragmentCompat() { frag.setTargetFragment(this, 0) frag.show(parentFragmentManager, FolderChooserDialogFragment.TAG) } else { - activity!!.showToast(getString(R.string.err_msg_external_storage_not_writable)) + requireActivity().showToast(getString(R.string.err_msg_external_storage_not_writable)) } } } fun setupCustomSaveLocationPreLollipop(file: File?) { + val activity = requireActivity() if (file == null) { - activity!!.showToast("Folder not selected") + activity.showToast("Folder not selected") } else { if (!file.canWrite()) { - activity!!.showToast("Folder not writable") + activity.showToast("Folder not writable") return } @@ -246,7 +250,7 @@ class SettingsFragment : PreferenceFragmentCompat() { @TargetApi(21) private fun setupCustomSaveLocationLollipop() { - if (ContextCompat.checkSelfPermission(activity!!, + if (ContextCompat.checkSelfPermission(requireActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), diff --git a/app/src/main/java/com/dp/logcatapp/fragments/settings/dialogs/FolderChooserDialogFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/settings/dialogs/FolderChooserDialogFragment.kt index e8fc3ca3..ab98b486 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/settings/dialogs/FolderChooserDialogFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/settings/dialogs/FolderChooserDialogFragment.kt @@ -35,7 +35,7 @@ class FolderChooserDialogFragment : BaseDialogFragment(), View.OnClickListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - recyclerViewAdapter = MyRecyclerViewAdapter(context!!, this) + recyclerViewAdapter = MyRecyclerViewAdapter(requireContext(), this) viewModel = getAndroidViewModel() viewModel.files.observe(this, Observer> { @@ -49,12 +49,13 @@ class FolderChooserDialogFragment : BaseDialogFragment(), View.OnClickListener { val rootView = inflateLayout(R.layout.folder_chooser) val recyclerView = rootView.findViewById(R.id.recyclerView) - layoutManager = LinearLayoutManager(activity!!) + val activity = requireActivity() + layoutManager = LinearLayoutManager(activity) recyclerView.layoutManager = layoutManager recyclerView.adapter = recyclerViewAdapter - return AlertDialog.Builder(activity!!) + return AlertDialog.Builder(activity) .setTitle("Select a folder") .setView(rootView) .setPositiveButton(getString(R.string.select)) { _, _ -> diff --git a/app/src/main/java/com/dp/logcatapp/fragments/shared/dialogs/CopyToClipboardDialogFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/shared/dialogs/CopyToClipboardDialogFragment.kt index 0ad3dc7a..40a07b78 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/shared/dialogs/CopyToClipboardDialogFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/shared/dialogs/CopyToClipboardDialogFragment.kt @@ -34,15 +34,16 @@ class CopyToClipboardDialogFragment : BaseDialogFragment(), DialogInterface.OnCl } override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - return AlertDialog.Builder(activity!!) + return AlertDialog.Builder(requireActivity()) .setTitle(R.string.copy_to_clipboard) .setItems(R.array.log_content_types, this) .create() } override fun onClick(dialog: DialogInterface, which: Int) { - val log = arguments!!.getParcelable(KEY_LOG)!! - val cm = activity!!.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager + val log = requireArguments().getParcelable(KEY_LOG)!! + val activity = requireActivity() + val cm = activity.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager val clip = when (which) { LogContentType.TAG.ordinal -> ClipData.newPlainText("Log Tag", log.tag) LogContentType.MESSAGE.ordinal -> ClipData.newPlainText("Log Msg", log.msg) @@ -52,6 +53,6 @@ class CopyToClipboardDialogFragment : BaseDialogFragment(), DialogInterface.OnCl else -> ClipData.newPlainText("Log TID", log.tid) } cm.setPrimaryClip(clip) - activity!!.showToast(getString(R.string.copied_to_clipboard)) + activity.showToast(getString(R.string.copied_to_clipboard)) } } \ No newline at end of file diff --git a/app/src/main/java/com/dp/logcatapp/fragments/shared/dialogs/FilterExclusionDialogFragment.kt b/app/src/main/java/com/dp/logcatapp/fragments/shared/dialogs/FilterExclusionDialogFragment.kt index 177b8d79..d67ec5bb 100644 --- a/app/src/main/java/com/dp/logcatapp/fragments/shared/dialogs/FilterExclusionDialogFragment.kt +++ b/app/src/main/java/com/dp/logcatapp/fragments/shared/dialogs/FilterExclusionDialogFragment.kt @@ -1,7 +1,8 @@ package com.dp.logcatapp.fragments.shared.dialogs import android.app.Dialog -import android.content.* +import android.content.DialogInterface +import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AlertDialog import com.dp.logcat.Log @@ -32,13 +33,13 @@ class FilterExclusionDialogFragment : BaseDialogFragment(), DialogInterface.OnCl } override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - return AlertDialog.Builder(activity!!) + return AlertDialog.Builder(requireActivity()) .setItems(R.array.filter_exclude, this) .create() } override fun onClick(dialog: DialogInterface, which: Int) { - val log = arguments!!.getParcelable(KEY_LOG)!! + val log = requireArguments().getParcelable(KEY_LOG)!! when (which) { LogContentType.FILTER.ordinal -> moveToFilterActivity(log, false) LogContentType.EXCLUDE.ordinal -> moveToFilterActivity(log, true) @@ -47,8 +48,8 @@ class FilterExclusionDialogFragment : BaseDialogFragment(), DialogInterface.OnCl } - fun moveToFilterActivity(log: Log, isExclusion: Boolean) { - val intent = Intent(activity!!, FiltersActivity::class.java) + private fun moveToFilterActivity(log: Log, isExclusion: Boolean) { + val intent = Intent(requireActivity(), FiltersActivity::class.java) intent.putExtra(FiltersActivity.EXTRA_EXCLUSIONS, isExclusion) intent.putExtra(FiltersActivity.KEY_LOG, log) startActivity(intent)