Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
darshanparajuli committed Mar 21, 2020
1 parent 6d2cf4e commit 7426b39
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -88,10 +88,11 @@ class FiltersFragment : BaseFragment() {

emptyMessage = rootView.findViewById(R.id.textViewEmpty)

val activity = requireActivity()
val recyclerView = rootView.findViewById<RecyclerView>(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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { _, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { _, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -48,7 +48,8 @@ class OnSavedBottomSheetDialogFragment : BottomSheetDialogFragment() {
}

rootView.findViewById<TextView>(R.id.actionShare).setOnClickListener {
ShareUtils.shareSavedLogs(context!!, uri, Utils.isUsingCustomSaveLocation(context!!))
val context = requireContext()
ShareUtils.shareSavedLogs(context, uri, Utils.isUsingCustomSaveLocation(context))
dismiss()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
Expand All @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<FileHolder>> {
Expand All @@ -49,12 +49,13 @@ class FolderChooserDialogFragment : BaseDialogFragment(), View.OnClickListener {
val rootView = inflateLayout(R.layout.folder_chooser)
val recyclerView = rootView.findViewById<RecyclerView>(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)) { _, _ ->
Expand Down
Loading

0 comments on commit 7426b39

Please sign in to comment.