From f218282e28816227e8aff3c343a8b7b85440ad53 Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 8 Mar 2018 22:16:02 -0800 Subject: [PATCH] Use require* in support library --- .../GlobalSettingsPreferenceFragment.kt | 4 +-- .../shadowsocks/ProfileConfigFragment.kt | 8 +++--- .../github/shadowsocks/ProfilesFragment.kt | 26 +++++++++---------- .../shadowsocks/acl/CustomRulesFragment.kt | 15 ++++++----- .../BottomSheetPreferenceDialogFragment.kt | 4 +-- .../PluginConfigurationDialogFragment.kt | 5 ++-- 6 files changed, 32 insertions(+), 30 deletions(-) diff --git a/mobile/src/main/java/com/github/shadowsocks/GlobalSettingsPreferenceFragment.kt b/mobile/src/main/java/com/github/shadowsocks/GlobalSettingsPreferenceFragment.kt index fae6cc75..0d07c43a 100644 --- a/mobile/src/main/java/com/github/shadowsocks/GlobalSettingsPreferenceFragment.kt +++ b/mobile/src/main/java/com/github/shadowsocks/GlobalSettingsPreferenceFragment.kt @@ -50,7 +50,7 @@ class GlobalSettingsPreferenceFragment : PreferenceFragmentCompatDividers() { boot.isChecked = BootReceiver.enabled val dba = findPreference(Key.directBootAware) - if (Build.VERSION.SDK_INT >= 24 && context!!.getSystemService(DevicePolicyManager::class.java) + if (Build.VERSION.SDK_INT >= 24 && requireContext().getSystemService(DevicePolicyManager::class.java) .storageEncryptionStatus == DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE_PER_USER) dba.onPreferenceChangeListener = directBootAwareListener else dba.parent!!.removePreference(dba) @@ -59,7 +59,7 @@ class GlobalSettingsPreferenceFragment : PreferenceFragmentCompatDividers() { tfo.setOnPreferenceChangeListener { _, value -> val result = TcpFastOpen.enabled(value as Boolean) if (result != null && result != "Success.") - Snackbar.make(activity!!.findViewById(R.id.snackbar), result, Snackbar.LENGTH_LONG).show() + Snackbar.make(requireActivity().findViewById(R.id.snackbar), result, Snackbar.LENGTH_LONG).show() value == TcpFastOpen.sendEnabled } if (!TcpFastOpen.supported) { diff --git a/mobile/src/main/java/com/github/shadowsocks/ProfileConfigFragment.kt b/mobile/src/main/java/com/github/shadowsocks/ProfileConfigFragment.kt index c664d84f..c93b8de7 100644 --- a/mobile/src/main/java/com/github/shadowsocks/ProfileConfigFragment.kt +++ b/mobile/src/main/java/com/github/shadowsocks/ProfileConfigFragment.kt @@ -65,7 +65,7 @@ class ProfileConfigFragment : PreferenceFragmentCompatDividers(), Toolbar.OnMenu override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) { preferenceManager.preferenceDataStore = DataStore.privateStore - val activity = activity!! + val activity = requireActivity() val profile = ProfileManager.getProfile(activity.intent.getIntExtra(Action.EXTRA_PROFILE_ID, -1)) if (profile == null) { activity.finish() @@ -134,7 +134,7 @@ class ProfileConfigFragment : PreferenceFragmentCompatDividers(), Toolbar.OnMenu ProfileManager.updateProfile(profile) ProfilesFragment.instance?.profilesAdapter?.deepRefreshId(profile.id) if (DataStore.profileId == profile.id && DataStore.directBootAware) DirectBoot.update() - activity!!.finish() + requireActivity().finish() } override fun onResume() { @@ -161,7 +161,7 @@ class ProfileConfigFragment : PreferenceFragmentCompatDividers(), Toolbar.OnMenu override fun onDisplayPreferenceDialog(preference: Preference) { if (preference.key == Key.pluginConfigure) { val intent = PluginManager.buildIntent(pluginConfiguration.selected, PluginContract.ACTION_CONFIGURE) - if (intent.resolveActivity(activity!!.packageManager) != null) + if (intent.resolveActivity(requireContext().packageManager) != null) startActivityForResult(intent.putExtra(PluginContract.EXTRA_OPTIONS, pluginConfiguration.selectedOptions.toString()), REQUEST_CODE_PLUGIN_CONFIGURE) else { showPluginEditor() @@ -183,7 +183,7 @@ class ProfileConfigFragment : PreferenceFragmentCompatDividers(), Toolbar.OnMenu override fun onMenuItemClick(item: MenuItem) = when (item.itemId) { R.id.action_delete -> { - val activity = activity!! + val activity = requireActivity() AlertDialog.Builder(activity) .setTitle(R.string.delete_confirm_prompt) .setPositiveButton(R.string.yes, { _, _ -> diff --git a/mobile/src/main/java/com/github/shadowsocks/ProfilesFragment.kt b/mobile/src/main/java/com/github/shadowsocks/ProfilesFragment.kt index 7ae8a8eb..45b78515 100644 --- a/mobile/src/main/java/com/github/shadowsocks/ProfilesFragment.kt +++ b/mobile/src/main/java/com/github/shadowsocks/ProfilesFragment.kt @@ -128,7 +128,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { itemView.setOnClickListener(this) val share = itemView.findViewById(R.id.share) share.setOnClickListener { - val popup = PopupMenu(activity!!, share) + val popup = PopupMenu(requireContext(), share) popup.menuInflater.inflate(R.menu.profile_share_popup, popup.menu) popup.setOnMenuItemClickListener(this) popup.show() @@ -173,10 +173,10 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { var adView = adView if (item.host == "198.199.101.152") { if (adView == null) { - val params = - LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) + val params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.WRAP_CONTENT) params.gravity = Gravity.CENTER_HORIZONTAL - val context = context!! + val context = requireContext() adView = AdView(context) adView.layoutParams = params adView.adUnitId = "ca-app-pub-9097031975646651/7760346322" @@ -208,7 +208,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { override fun onMenuItemClick(item: MenuItem): Boolean = when (item.itemId) { R.id.action_qr_code_nfc -> { - fragmentManager!!.beginTransaction().add(QRCodeDialog(this.item.toString()), "") + requireFragmentManager().beginTransaction().add(QRCodeDialog(this.item.toString()), "") .commitAllowingStateLoss() true } @@ -230,7 +230,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { override fun onBindViewHolder(holder: ProfileViewHolder, position: Int) = holder.bind(profiles[position]) override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProfileViewHolder = ProfileViewHolder( - LayoutInflater.from(parent!!.context).inflate(R.layout.layout_profile, parent, false)) + LayoutInflater.from(parent.context).inflate(R.layout.layout_profile, parent, false)) override fun getItemCount(): Int = profiles.size override fun getItemId(position: Int): Long = profiles[position].id.toLong() @@ -305,7 +305,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { private var txTotal: Long = 0L private var rxTotal: Long = 0L - private val clipboard by lazy { activity!!.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager } + private val clipboard by lazy { requireContext().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager } private fun startConfig(id: Int) = startActivity(Intent(context, ProfileConfigActivity::class.java) .putExtra(Action.EXTRA_PROFILE_ID, id)) @@ -331,7 +331,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { profilesList.itemAnimator = animator profilesList.adapter = profilesAdapter instance = this - undoManager = UndoSnackbarManager(activity!!.findViewById(R.id.snackbar), + undoManager = UndoSnackbarManager(requireActivity().findViewById(R.id.snackbar), profilesAdapter::undo, profilesAdapter::commit) ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP or ItemTouchHelper.DOWN, ItemTouchHelper.START or ItemTouchHelper.END) { @@ -351,7 +351,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { profilesAdapter.move(viewHolder.adapterPosition, target.adapterPosition) return true } - override fun clearView(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder?) { + override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { super.clearView(recyclerView, viewHolder) profilesAdapter.commitMove() } @@ -369,13 +369,13 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { val profiles = Profile.findAll(clipboard.primaryClip.getItemAt(0).text).toList() if (profiles.isNotEmpty()) { profiles.forEach { ProfileManager.createProfile(it) } - Snackbar.make(activity!!.findViewById(R.id.snackbar), R.string.action_import_msg, + Snackbar.make(requireActivity().findViewById(R.id.snackbar), R.string.action_import_msg, Snackbar.LENGTH_LONG).show() return true } } catch (_: IndexOutOfBoundsException) { } - Snackbar.make(activity!!.findViewById(R.id.snackbar), R.string.action_import_err, Snackbar.LENGTH_LONG) - .show() + Snackbar.make(requireActivity().findViewById(R.id.snackbar), R.string.action_import_err, + Snackbar.LENGTH_LONG).show() true } R.id.action_manual_settings -> { @@ -384,7 +384,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { } R.id.action_export -> { val profiles = ProfileManager.getAllProfiles() - Snackbar.make(activity!!.findViewById(R.id.snackbar), if (profiles != null) { + Snackbar.make(requireActivity().findViewById(R.id.snackbar), if (profiles != null) { clipboard.primaryClip = ClipData.newPlainText(null, profiles.joinToString("\n")) R.string.action_export_msg } else R.string.action_export_err, Snackbar.LENGTH_LONG).show() diff --git a/mobile/src/main/java/com/github/shadowsocks/acl/CustomRulesFragment.kt b/mobile/src/main/java/com/github/shadowsocks/acl/CustomRulesFragment.kt index 0c5065f5..f244acaf 100644 --- a/mobile/src/main/java/com/github/shadowsocks/acl/CustomRulesFragment.kt +++ b/mobile/src/main/java/com/github/shadowsocks/acl/CustomRulesFragment.kt @@ -80,7 +80,7 @@ class CustomRulesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener, private lateinit var positive: Button init { - val activity = activity!! + val activity = requireActivity() val view = activity.layoutInflater.inflate(R.layout.dialog_acl_rule, null) templateSelector = view.findViewById(R.id.template_selector) editText = view.findViewById(R.id.content) @@ -360,7 +360,7 @@ class CustomRulesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener, private lateinit var list: RecyclerView private var mode: ActionMode? = null private lateinit var undoManager: UndoSnackbarManager - private val clipboard by lazy { activity!!.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager } + private val clipboard by lazy { requireContext().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager } private fun onSelectedItemsUpdated() { if (selectedItems.isEmpty()) mode?.finish() else if (mode == null) mode = toolbar.startActionMode(this) @@ -383,12 +383,13 @@ class CustomRulesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener, toolbar.setTitle(R.string.custom_rules) toolbar.inflateMenu(R.menu.custom_rules_menu) toolbar.setOnMenuItemClickListener(this) + val activity = requireActivity() list = view.findViewById(R.id.list) list.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false) list.itemAnimator = DefaultItemAnimator() list.adapter = adapter view.findViewById(R.id.fastscroller).setRecyclerView(list) - undoManager = UndoSnackbarManager(activity!!.findViewById(R.id.snackbar), adapter::undo) + undoManager = UndoSnackbarManager(activity.findViewById(R.id.snackbar), adapter::undo) ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.START or ItemTouchHelper.END) { override fun getSwipeDirs(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int = if (isEnabled && selectedItems.isEmpty()) super.getSwipeDirs(recyclerView, viewHolder) else 0 @@ -439,8 +440,8 @@ class CustomRulesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener, try { adapter.addToProxy(clipboard.primaryClip.getItemAt(0).text.toString()) != null } catch (exc: Exception) { - Snackbar.make(activity!!.findViewById(R.id.snackbar), R.string.action_import_err, Snackbar.LENGTH_LONG) - .show() + Snackbar.make(requireActivity().findViewById(R.id.snackbar), R.string.action_import_err, + Snackbar.LENGTH_LONG).show() app.track(exc) } true @@ -462,7 +463,7 @@ class CustomRulesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener, } override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean { - val activity = activity!! + val activity = requireActivity() val window = activity.window // In the end material_grey_100 is used for background, see AppCompatDrawableManager (very complicated) if (Build.VERSION.SDK_INT >= 23) { @@ -495,7 +496,7 @@ class CustomRulesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener, else -> false } override fun onDestroyActionMode(mode: ActionMode) { - val activity = activity!! + val activity = requireActivity() val window = activity.window window.statusBarColor = ContextCompat.getColor(activity, activity.theme.resolveResourceId(android.R.attr.statusBarColor)) diff --git a/mobile/src/main/java/com/github/shadowsocks/preference/BottomSheetPreferenceDialogFragment.kt b/mobile/src/main/java/com/github/shadowsocks/preference/BottomSheetPreferenceDialogFragment.kt index ffd0d446..4399bd45 100644 --- a/mobile/src/main/java/com/github/shadowsocks/preference/BottomSheetPreferenceDialogFragment.kt +++ b/mobile/src/main/java/com/github/shadowsocks/preference/BottomSheetPreferenceDialogFragment.kt @@ -101,8 +101,8 @@ class BottomSheetPreferenceDialogFragment : PreferenceDialogFragmentCompat() { private var clickedIndex = -1 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - val activity = activity - val dialog = BottomSheetDialog(activity!!, theme) + val activity = requireActivity() + val dialog = BottomSheetDialog(activity, theme) val recycler = RecyclerView(activity) val padding = resources.getDimensionPixelOffset(R.dimen.bottom_sheet_padding) recycler.setPadding(0, padding, 0, padding) diff --git a/mobile/src/main/java/com/github/shadowsocks/preference/PluginConfigurationDialogFragment.kt b/mobile/src/main/java/com/github/shadowsocks/preference/PluginConfigurationDialogFragment.kt index 0cf060bb..af9668ee 100644 --- a/mobile/src/main/java/com/github/shadowsocks/preference/PluginConfigurationDialogFragment.kt +++ b/mobile/src/main/java/com/github/shadowsocks/preference/PluginConfigurationDialogFragment.kt @@ -40,8 +40,9 @@ class PluginConfigurationDialogFragment : EditTextPreferenceDialogFragmentCompat super.onPrepareDialogBuilder(builder) val intent = PluginManager.buildIntent(arguments!!.getString(PLUGIN_ID_FRAGMENT_TAG), PluginContract.ACTION_HELP) - if (intent.resolveActivity(activity!!.packageManager) != null) builder.setNeutralButton("?", { _, _ -> - activity!!.startActivityForResult(intent.putExtra(PluginContract.EXTRA_OPTIONS, editText.text.toString()), + val activity = requireActivity() + if (intent.resolveActivity(activity.packageManager) != null) builder.setNeutralButton("?", { _, _ -> + activity.startActivityForResult(intent.putExtra(PluginContract.EXTRA_OPTIONS, editText.text.toString()), ProfileConfigActivity.REQUEST_CODE_PLUGIN_HELP) }) }