diff --git a/V2rayNG/app/build.gradle b/V2rayNG/app/build.gradle index 029a7603f..4696c7c9d 100644 --- a/V2rayNG/app/build.gradle +++ b/V2rayNG/app/build.gradle @@ -64,6 +64,10 @@ android { 1000000 + android.defaultConfig.versionCode } } + + buildFeatures { + viewBinding true + } } dependencies { diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/LogcatActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/LogcatActivity.kt index 8b4895d77..146884fa9 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/LogcatActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/LogcatActivity.kt @@ -9,9 +9,9 @@ import android.view.MenuItem import android.view.View import com.v2ray.ang.AppConfig.ANG_PACKAGE import com.v2ray.ang.R +import com.v2ray.ang.databinding.ActivityLogcatBinding import com.v2ray.ang.extension.toast import com.v2ray.ang.util.Utils -import kotlinx.android.synthetic.main.activity_logcat.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch @@ -19,10 +19,13 @@ import java.io.IOException import java.util.LinkedHashSet class LogcatActivity : BaseActivity() { + private lateinit var binding: ActivityLogcatBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_logcat) + binding = ActivityLogcatBinding.inflate(layoutInflater) + val view = binding.root + setContentView(view) title = getString(R.string.title_logcat) @@ -33,7 +36,7 @@ class LogcatActivity : BaseActivity() { private fun logcat(shouldFlushLog: Boolean) { try { - pb_waiting.visibility = View.VISIBLE + binding.pbWaiting.visibility = View.VISIBLE GlobalScope.launch(Dispatchers.Default) { if (shouldFlushLog) { @@ -56,10 +59,10 @@ class LogcatActivity : BaseActivity() { // val allText = bufferedReader.use(BufferedReader::readText) val allText = process.inputStream.bufferedReader().use { it.readText() } launch(Dispatchers.Main) { - tv_logcat.text = allText - tv_logcat.movementMethod = ScrollingMovementMethod() - pb_waiting.visibility = View.GONE - Handler(Looper.getMainLooper()).post { sv_logcat.fullScroll(View.FOCUS_DOWN) } + binding.tvLogcat.text = allText + binding.tvLogcat.movementMethod = ScrollingMovementMethod() + binding.pbWaiting.visibility = View.GONE + Handler(Looper.getMainLooper()).post { binding.svLogcat.fullScroll(View.FOCUS_DOWN) } } } } catch (e: IOException) { @@ -74,7 +77,7 @@ class LogcatActivity : BaseActivity() { override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) { R.id.copy_all -> { - Utils.setClipboard(this, tv_logcat.text.toString()) + Utils.setClipboard(this, binding.tvLogcat.text.toString()) toast(R.string.toast_success) true } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt index 17a2c1a76..5a7b1f5fe 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt @@ -23,6 +23,7 @@ import com.v2ray.ang.AppConfig import com.v2ray.ang.AppConfig.ANG_PACKAGE import com.v2ray.ang.BuildConfig import com.v2ray.ang.R +import com.v2ray.ang.databinding.ActivityMainBinding import com.v2ray.ang.dto.EConfigType import com.v2ray.ang.extension.toast import com.v2ray.ang.helper.SimpleItemTouchHelperCallback @@ -31,7 +32,6 @@ import com.v2ray.ang.util.AngConfigManager import com.v2ray.ang.util.MmkvManager import com.v2ray.ang.util.Utils import com.v2ray.ang.viewmodel.MainViewModel -import kotlinx.android.synthetic.main.activity_main.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch @@ -49,6 +49,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList private const val REQUEST_FILE_CHOOSER = 2 private const val REQUEST_SCAN_URL = 3 } + private lateinit var binding: ActivityMainBinding private val adapter by lazy { MainRecyclerAdapter(this) } private val mainStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_MAIN, MMKV.MULTI_PROCESS_MODE) } @@ -58,11 +59,13 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_main) + binding = ActivityMainBinding.inflate(layoutInflater) + val view = binding.root + setContentView(view) title = getString(R.string.title_server) - setSupportActionBar(toolbar) + setSupportActionBar(binding.toolbar) - fab.setOnClickListener { + binding.fab.setOnClickListener { if (mainViewModel.isRunning.value == true) { Utils.stopVService(this) } else if (settingsStorage?.decodeString(AppConfig.PREF_MODE) ?: "VPN" == "VPN") { @@ -76,30 +79,30 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList startV2Ray() } } - layout_test.setOnClickListener { + binding.layoutTest.setOnClickListener { if (mainViewModel.isRunning.value == true) { - tv_test_state.text = getString(R.string.connection_test_testing) + binding.tvTestState.text = getString(R.string.connection_test_testing) mainViewModel.testCurrentServerRealPing() } else { // tv_test_state.text = getString(R.string.connection_test_fail) } } - recycler_view.setHasFixedSize(true) - recycler_view.layoutManager = LinearLayoutManager(this) - recycler_view.adapter = adapter + binding.recyclerView.setHasFixedSize(true) + binding.recyclerView.layoutManager = LinearLayoutManager(this) + binding.recyclerView.adapter = adapter val callback = SimpleItemTouchHelperCallback(adapter) mItemTouchHelper = ItemTouchHelper(callback) - mItemTouchHelper?.attachToRecyclerView(recycler_view) + mItemTouchHelper?.attachToRecyclerView(binding.recyclerView) val toggle = ActionBarDrawerToggle( - this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) - drawer_layout.addDrawerListener(toggle) + this, binding.drawerLayout, binding.toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) + binding.drawerLayout.addDrawerListener(toggle) toggle.syncState() - nav_view.setNavigationItemSelectedListener(this) - version.text = "v${BuildConfig.VERSION_NAME} (${Libv2ray.checkVersionX()})" + binding.navView.setNavigationItemSelectedListener(this) + binding.version.text = "v${BuildConfig.VERSION_NAME} (${Libv2ray.checkVersionX()})" setupViewModelObserver() migrateLegacy() @@ -114,18 +117,18 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList adapter.notifyDataSetChanged() } }) - mainViewModel.updateTestResultAction.observe(this, { tv_test_state.text = it }) + mainViewModel.updateTestResultAction.observe(this, { binding.tvTestState.text = it }) mainViewModel.isRunning.observe(this, { val isRunning = it ?: return@observe adapter.isRunning = isRunning if (isRunning) { - fab.setImageResource(R.drawable.ic_v) - tv_test_state.text = getString(R.string.connection_connected) - layout_test.isFocusable = true + binding.fab.setImageResource(R.drawable.ic_v) + binding.tvTestState.text = getString(R.string.connection_connected) + binding.layoutTest.isFocusable = true } else { - fab.setImageResource(R.drawable.ic_v_idle) - tv_test_state.text = getString(R.string.connection_not_connected) - layout_test.isFocusable = false + binding.fab.setImageResource(R.drawable.ic_v_idle) + binding.tvTestState.text = getString(R.string.connection_not_connected) + binding.layoutTest.isFocusable = false } hideCircle() }) @@ -505,7 +508,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList } fun showCircle() { - fabProgressCircle?.show() + binding.fabProgressCircle?.show() } fun hideCircle() { @@ -513,8 +516,8 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList Observable.timer(300, TimeUnit.MILLISECONDS) .observeOn(AndroidSchedulers.mainThread()) .subscribe { - if (fabProgressCircle.isShown) { - fabProgressCircle.hide() + if (binding.fabProgressCircle.isShown) { + binding.fabProgressCircle.hide() } } } catch (e: Exception) { @@ -522,8 +525,8 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList } override fun onBackPressed() { - if (drawer_layout.isDrawerOpen(GravityCompat.START)) { - drawer_layout.closeDrawer(GravityCompat.START) + if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) { + binding.drawerLayout.closeDrawer(GravityCompat.START) } else { super.onBackPressed() } @@ -553,7 +556,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList startActivity(Intent(this, LogcatActivity::class.java)) } } - drawer_layout.closeDrawer(GravityCompat.START) + binding.drawerLayout.closeDrawer(GravityCompat.START) return true } } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainRecyclerAdapter.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainRecyclerAdapter.kt index 8cf548a3a..5538c695d 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainRecyclerAdapter.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainRecyclerAdapter.kt @@ -12,6 +12,9 @@ import com.google.gson.Gson import com.tencent.mmkv.MMKV import com.v2ray.ang.AppConfig import com.v2ray.ang.R +import com.v2ray.ang.databinding.ItemQrcodeBinding +import com.v2ray.ang.databinding.ItemRecyclerFooterBinding +import com.v2ray.ang.databinding.ItemRecyclerMainBinding import com.v2ray.ang.dto.EConfigType import com.v2ray.ang.dto.SubscriptionItem import com.v2ray.ang.extension.toast @@ -21,8 +24,6 @@ import com.v2ray.ang.service.V2RayServiceManager import com.v2ray.ang.util.AngConfigManager import com.v2ray.ang.util.MmkvManager import com.v2ray.ang.util.Utils -import kotlinx.android.synthetic.main.item_qrcode.view.* -import kotlinx.android.synthetic.main.item_recycler_main.view.* import rx.Observable import rx.android.schedulers.AndroidSchedulers import java.util.concurrent.TimeUnit @@ -51,34 +52,34 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter try { when (i) { @@ -86,9 +87,9 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter { @@ -107,7 +108,7 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter - MainViewHolder(LayoutInflater.from(parent.context) - .inflate(R.layout.item_recycler_main, parent, false)) + MainViewHolder(ItemRecyclerMainBinding.inflate(LayoutInflater.from(parent.context), parent, false)) else -> - FooterViewHolder(LayoutInflater.from(parent.context) - .inflate(R.layout.item_recycler_footer, parent, false)) + FooterViewHolder(ItemRecyclerFooterBinding.inflate(LayoutInflater.from(parent.context), parent, false)) } } @@ -182,40 +181,21 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter? = null @@ -43,12 +44,14 @@ class PerAppProxyActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_bypass_list) + binding = ActivityBypassListBinding.inflate(layoutInflater) + val view = binding.root + setContentView(view) supportActionBar?.setDisplayHomeAsUpEnabled(true) val dividerItemDecoration = DividerItemDecoration(this, LinearLayoutManager.VERTICAL) - recycler_view.addItemDecoration(dividerItemDecoration) + binding.recyclerView.addItemDecoration(dividerItemDecoration) val blacklist = defaultSharedPreferences.getStringSet(AppConfig.PREF_PER_APP_PROXY_SET, null) @@ -90,20 +93,20 @@ class PerAppProxyActivity : BaseActivity() { .subscribe { appsAll = it adapter = PerAppProxyAdapter(this, it, blacklist) - recycler_view.adapter = adapter - pb_waiting.visibility = View.GONE + binding.recyclerView.adapter = adapter + binding.pbWaiting.visibility = View.GONE } - recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() { + binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { var dst = 0 val threshold = resources.getDimensionPixelSize(R.dimen.bypass_list_header_height) * 3 override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { dst += dy if (dst > threshold) { - header_view.hide() + binding.headerView.hide() dst = 0 } else if (dst < -20) { - header_view.show() + binding.headerView.show() dst = 0 } } @@ -139,17 +142,17 @@ class PerAppProxyActivity : BaseActivity() { } }) - switch_per_app_proxy.setOnCheckedChangeListener { _, isChecked -> + binding.switchPerAppProxy.setOnCheckedChangeListener { _, isChecked -> defaultSharedPreferences.edit().putBoolean(AppConfig.PREF_PER_APP_PROXY, isChecked).apply() } - switch_per_app_proxy.isChecked = defaultSharedPreferences.getBoolean(AppConfig.PREF_PER_APP_PROXY, false) + binding.switchPerAppProxy.isChecked = defaultSharedPreferences.getBoolean(AppConfig.PREF_PER_APP_PROXY, false) - switch_bypass_apps.setOnCheckedChangeListener { _, isChecked -> + binding.switchBypassApps.setOnCheckedChangeListener { _, isChecked -> defaultSharedPreferences.edit().putBoolean(AppConfig.PREF_BYPASS_APPS, isChecked).apply() } - switch_bypass_apps.isChecked = defaultSharedPreferences.getBoolean(AppConfig.PREF_BYPASS_APPS, false) + binding.switchBypassApps.isChecked = defaultSharedPreferences.getBoolean(AppConfig.PREF_BYPASS_APPS, false) - et_search.setOnEditorActionListener { v, actionId, event -> + binding.etSearch.setOnEditorActionListener { v, actionId, event -> if (actionId == EditorInfo.IME_ACTION_SEARCH) { //hide var imm: InputMethodManager = v.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager @@ -169,7 +172,7 @@ class PerAppProxyActivity : BaseActivity() { } } adapter = PerAppProxyAdapter(this, apps, adapter?.blacklist) - recycler_view.adapter = adapter + binding.recyclerView.adapter = adapter adapter?.notifyDataSetChanged() true } else { @@ -247,7 +250,7 @@ class PerAppProxyActivity : BaseActivity() { adapter?.blacklist!!.clear() - if (switch_bypass_apps.isChecked) { + if (binding.switchBypassApps.isChecked) { adapter?.let { it.apps.forEach block@{ val packageName = it.packageName diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/PerAppProxyAdapter.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/PerAppProxyAdapter.kt index 234594a04..ec7e1242c 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/PerAppProxyAdapter.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/PerAppProxyAdapter.kt @@ -6,8 +6,8 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import com.v2ray.ang.R +import com.v2ray.ang.databinding.ItemRecyclerBypassListBinding import com.v2ray.ang.dto.AppInfo -import kotlinx.android.synthetic.main.item_recycler_bypass_list.view.* import java.util.* class PerAppProxyAdapter(val activity: BaseActivity, val apps: List, blacklist: MutableSet?) : @@ -18,8 +18,7 @@ class PerAppProxyAdapter(val activity: BaseActivity, val apps: List, bl private const val VIEW_TYPE_ITEM = 1 } - private var mActivity: BaseActivity = activity - val blacklist = if (blacklist == null) HashSet() else HashSet(blacklist) + val blacklist = if (blacklist == null) HashSet() else HashSet(blacklist) override fun onBindViewHolder(holder: BaseViewHolder, position: Int) { if (holder is AppViewHolder) { @@ -43,8 +42,7 @@ class PerAppProxyAdapter(val activity: BaseActivity, val apps: List, bl // VIEW_TYPE_ITEM -> AppViewHolder(ctx.layoutInflater // .inflate(R.layout.item_recycler_bypass_list, parent, false)) - else -> AppViewHolder(LayoutInflater.from(ctx) - .inflate(R.layout.item_recycler_bypass_list, parent, false)) + else -> AppViewHolder(ItemRecyclerBypassListBinding.inflate(LayoutInflater.from(ctx), parent, false)) } } @@ -53,30 +51,25 @@ class PerAppProxyAdapter(val activity: BaseActivity, val apps: List, bl open class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) - inner class AppViewHolder(itemView: View) : BaseViewHolder(itemView), + inner class AppViewHolder(private val itemBypassBinding: ItemRecyclerBypassListBinding) : BaseViewHolder(itemBypassBinding.root), View.OnClickListener { private val inBlacklist: Boolean get() = blacklist.contains(appInfo.packageName) private lateinit var appInfo: AppInfo - val icon = itemView.icon!! - val name = itemView.name!! - val package_name = itemView.package_name!! - val checkBox = itemView.check_box!! - fun bind(appInfo: AppInfo) { this.appInfo = appInfo - icon.setImageDrawable(appInfo.appIcon) + itemBypassBinding.icon.setImageDrawable(appInfo.appIcon) // name.text = appInfo.appName - checkBox.isChecked = inBlacklist - package_name.text = appInfo.packageName + itemBypassBinding.checkBox.isChecked = inBlacklist + itemBypassBinding.packageName.text = appInfo.packageName if (appInfo.isSystemApp) { - name.text = String.format("** %1s", appInfo.appName) - name.setTextColor(Color.RED) + itemBypassBinding.name.text = String.format("** %1s", appInfo.appName) + itemBypassBinding.name.setTextColor(Color.RED) } else { - name.text = appInfo.appName - name.setTextColor(Color.DKGRAY) + itemBypassBinding.name.text = appInfo.appName + itemBypassBinding.name.setTextColor(Color.DKGRAY) } itemView.setOnClickListener(this) @@ -85,10 +78,10 @@ class PerAppProxyAdapter(val activity: BaseActivity, val apps: List, bl override fun onClick(v: View?) { if (inBlacklist) { blacklist.remove(appInfo.packageName) - checkBox.isChecked = false + itemBypassBinding.checkBox.isChecked = false } else { blacklist.add(appInfo.packageName) - checkBox.isChecked = true + itemBypassBinding.checkBox.isChecked = true } } } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/RoutingSettingsActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/RoutingSettingsActivity.kt index d672a331b..7b04f41ee 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/RoutingSettingsActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/RoutingSettingsActivity.kt @@ -5,17 +5,21 @@ import android.os.Bundle import com.v2ray.ang.R import androidx.fragment.app.Fragment import com.v2ray.ang.AppConfig -import kotlinx.android.synthetic.main.activity_routing_settings.* +import com.v2ray.ang.databinding.ActivityRoutingSettingsBinding class RoutingSettingsActivity : BaseActivity() { + private lateinit var binding: ActivityRoutingSettingsBinding + private val titles: Array by lazy { resources.getStringArray(R.array.routing_tag) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_routing_settings) + binding = ActivityRoutingSettingsBinding.inflate(layoutInflater) + val view = binding.root + setContentView(view) title = getString(R.string.routing_settings_title) supportActionBar?.setDisplayHomeAsUpEnabled(true) @@ -26,8 +30,8 @@ class RoutingSettingsActivity : BaseActivity() { fragments.add(RoutingSettingsFragment().newInstance(AppConfig.PREF_V2RAY_ROUTING_BLOCKED)) val adapter = FragmentAdapter(supportFragmentManager, fragments, titles.toList()) - viewpager?.adapter = adapter - tablayout.setTabTextColors(Color.BLACK, Color.RED) - tablayout.setupWithViewPager(viewpager) + binding.viewpager.adapter = adapter + binding.tablayout.setTabTextColors(Color.BLACK, Color.RED) + binding.tablayout.setupWithViewPager(binding.viewpager) } } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerCustomConfigActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerCustomConfigActivity.kt index 216130772..e445b7444 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerCustomConfigActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/ServerCustomConfigActivity.kt @@ -10,16 +10,17 @@ import com.blacksquircle.ui.language.json.JsonLanguage import com.google.gson.Gson import com.tencent.mmkv.MMKV import com.v2ray.ang.R +import com.v2ray.ang.databinding.ActivityServerCustomConfigBinding import com.v2ray.ang.dto.EConfigType import com.v2ray.ang.dto.ServerConfig import com.v2ray.ang.dto.V2rayConfig import com.v2ray.ang.extension.toast import com.v2ray.ang.util.MmkvManager import com.v2ray.ang.util.Utils -import kotlinx.android.synthetic.main.activity_server_custom_config.* import me.drakeet.support.toast.ToastCompat class ServerCustomConfigActivity : BaseActivity() { + private lateinit var binding: ActivityServerCustomConfigBinding private val mainStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_MAIN, MMKV.MULTI_PROCESS_MODE) } private val serverRawStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_SERVER_RAW, MMKV.MULTI_PROCESS_MODE) } @@ -32,10 +33,12 @@ class ServerCustomConfigActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_server_custom_config) + binding = ActivityServerCustomConfigBinding.inflate(layoutInflater) + val view = binding.root + setContentView(view) title = getString(R.string.title_server) - editor.language = JsonLanguage() + binding.editor.language = JsonLanguage() val config = MmkvManager.decodeServerConfig(editGuid) if (config != null) { bindingServer(config) @@ -49,12 +52,12 @@ class ServerCustomConfigActivity : BaseActivity() { * bingding seleced server config */ private fun bindingServer(config: ServerConfig): Boolean { - et_remarks.text = Utils.getEditable(config.remarks) + binding.etRemarks.text = Utils.getEditable(config.remarks) val raw = serverRawStorage?.decodeString(editGuid) if (raw.isNullOrBlank()) { - editor.setTextContent(Utils.getEditable(config.fullConfig?.toPrettyPrinting().orEmpty())) + binding.editor.setTextContent(Utils.getEditable(config.fullConfig?.toPrettyPrinting().orEmpty())) } else { - editor.setTextContent(Utils.getEditable(raw)) + binding.editor.setTextContent(Utils.getEditable(raw)) } return true } @@ -63,7 +66,7 @@ class ServerCustomConfigActivity : BaseActivity() { * clear or init server config */ private fun clearServer(): Boolean { - et_remarks.text = null + binding.etRemarks.text = null return true } @@ -71,13 +74,13 @@ class ServerCustomConfigActivity : BaseActivity() { * save server config */ private fun saveServer(): Boolean { - if (TextUtils.isEmpty(et_remarks.text.toString())) { + if (TextUtils.isEmpty(binding.etRemarks.text.toString())) { toast(R.string.server_lab_remarks) return false } val v2rayConfig = try { - Gson().fromJson(editor.text.toString(), V2rayConfig::class.java) + Gson().fromJson(binding.editor.text.toString(), V2rayConfig::class.java) } catch (e: Exception) { e.printStackTrace() ToastCompat.makeText(this, "${getString(R.string.toast_malformed_josn)} ${e.cause?.message}", Toast.LENGTH_LONG).show() @@ -85,11 +88,11 @@ class ServerCustomConfigActivity : BaseActivity() { } val config = MmkvManager.decodeServerConfig(editGuid) ?: ServerConfig.create(EConfigType.CUSTOM) - config.remarks = et_remarks.text.toString().trim() + config.remarks = binding.etRemarks.text.toString().trim() config.fullConfig = v2rayConfig MmkvManager.encodeServerConfig(editGuid, config) - serverRawStorage?.encode(editGuid, editor.text.toString()) + serverRawStorage?.encode(editGuid, binding.editor.text.toString()) toast(R.string.toast_success) finish() return true diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SubEditActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SubEditActivity.kt index 2163dbbb5..dbc048951 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SubEditActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SubEditActivity.kt @@ -8,13 +8,14 @@ import android.view.MenuItem import com.google.gson.Gson import com.tencent.mmkv.MMKV import com.v2ray.ang.R +import com.v2ray.ang.databinding.ActivitySubEditBinding import com.v2ray.ang.dto.SubscriptionItem import com.v2ray.ang.extension.toast import com.v2ray.ang.util.MmkvManager import com.v2ray.ang.util.Utils -import kotlinx.android.synthetic.main.activity_sub_edit.* class SubEditActivity : BaseActivity() { + private lateinit var binding: ActivitySubEditBinding var del_config: MenuItem? = null var save_config: MenuItem? = null @@ -24,7 +25,9 @@ class SubEditActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_sub_edit) + binding = ActivitySubEditBinding.inflate(layoutInflater) + val view = binding.root + setContentView(view) title = getString(R.string.title_sub_setting) val json = subStorage?.decodeString(editSubId) @@ -40,8 +43,8 @@ class SubEditActivity : BaseActivity() { * bingding seleced server config */ private fun bindingServer(subItem: SubscriptionItem): Boolean { - et_remarks.text = Utils.getEditable(subItem.remarks) - et_url.text = Utils.getEditable(subItem.url) + binding.etRemarks.text = Utils.getEditable(subItem.remarks) + binding.etUrl.text = Utils.getEditable(subItem.url) return true } @@ -50,8 +53,8 @@ class SubEditActivity : BaseActivity() { * clear or init server config */ private fun clearServer(): Boolean { - et_remarks.text = null - et_url.text = null + binding.etRemarks.text = null + binding.etUrl.text = null return true } @@ -70,8 +73,8 @@ class SubEditActivity : BaseActivity() { subItem = SubscriptionItem() } - subItem.remarks = et_remarks.text.toString() - subItem.url = et_url.text.toString() + subItem.remarks = binding.etRemarks.text.toString() + subItem.url = binding.etUrl.text.toString() if (TextUtils.isEmpty(subItem.remarks)) { toast(R.string.sub_setting_remarks) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SubSettingActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SubSettingActivity.kt index a007814c1..32abe229c 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SubSettingActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SubSettingActivity.kt @@ -4,26 +4,29 @@ import android.content.Intent import android.view.Menu import android.view.MenuItem import com.v2ray.ang.R -import kotlinx.android.synthetic.main.activity_sub_setting.* import android.os.Bundle import androidx.recyclerview.widget.LinearLayoutManager +import com.v2ray.ang.databinding.ActivitySubSettingBinding import com.v2ray.ang.dto.SubscriptionItem import com.v2ray.ang.util.MmkvManager class SubSettingActivity : BaseActivity() { + private lateinit var binding: ActivitySubSettingBinding var subscriptions:List> = listOf() private val adapter by lazy { SubSettingRecyclerAdapter(this) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_sub_setting) + binding = ActivitySubSettingBinding.inflate(layoutInflater) + val view = binding.root + setContentView(view) title = getString(R.string.title_sub_setting) - recycler_view.setHasFixedSize(true) - recycler_view.layoutManager = LinearLayoutManager(this) - recycler_view.adapter = adapter + binding.recyclerView.setHasFixedSize(true) + binding.recyclerView.layoutManager = LinearLayoutManager(this) + binding.recyclerView.adapter = adapter supportActionBar?.setDisplayHomeAsUpEnabled(true) } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SubSettingRecyclerAdapter.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SubSettingRecyclerAdapter.kt index ef374229c..80a20c105 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SubSettingRecyclerAdapter.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/SubSettingRecyclerAdapter.kt @@ -4,44 +4,32 @@ import android.content.Intent import android.graphics.Color import androidx.recyclerview.widget.RecyclerView import android.view.LayoutInflater -import android.view.View import android.view.ViewGroup -import com.v2ray.ang.R -import kotlinx.android.synthetic.main.item_recycler_sub_setting.view.* +import com.v2ray.ang.databinding.ItemRecyclerSubSettingBinding -class SubSettingRecyclerAdapter(val activity: SubSettingActivity) : RecyclerView.Adapter() { +class SubSettingRecyclerAdapter(val activity: SubSettingActivity) : RecyclerView.Adapter() { private var mActivity: SubSettingActivity = activity override fun getItemCount() = mActivity.subscriptions.size - override fun onBindViewHolder(holder: BaseViewHolder, position: Int) { - if (holder is MainViewHolder) { - val subId = mActivity.subscriptions[position].first - val subItem = mActivity.subscriptions[position].second - holder.name.text = subItem.remarks - holder.url.text = subItem.url - holder.itemView.setBackgroundColor(Color.TRANSPARENT) + override fun onBindViewHolder(holder: MainViewHolder, position: Int) { + val subId = mActivity.subscriptions[position].first + val subItem = mActivity.subscriptions[position].second + holder.itemSubSettingBinding.tvName.text = subItem.remarks + holder.itemSubSettingBinding.tvUrl.text = subItem.url + holder.itemView.setBackgroundColor(Color.TRANSPARENT) - holder.layout_edit.setOnClickListener { - mActivity.startActivity(Intent(mActivity, SubEditActivity::class.java) - .putExtra("subId", subId) - ) - } + holder.itemSubSettingBinding.layoutEdit.setOnClickListener { + mActivity.startActivity(Intent(mActivity, SubEditActivity::class.java) + .putExtra("subId", subId) + ) } } - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder { - return MainViewHolder(LayoutInflater.from(parent.context) - .inflate(R.layout.item_recycler_sub_setting, parent, false)) - } - - open class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) - - class MainViewHolder(itemView: View) : BaseViewHolder(itemView) { - val name = itemView.tv_name!! - val url = itemView.tv_url!! - val layout_edit = itemView.layout_edit!! + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHolder { + return MainViewHolder(ItemRecyclerSubSettingBinding.inflate(LayoutInflater.from(parent.context), parent, false)) } + class MainViewHolder(val itemSubSettingBinding: ItemRecyclerSubSettingBinding) : RecyclerView.ViewHolder(itemSubSettingBinding.root) } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/TaskerActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/TaskerActivity.kt index 2b8c16739..8ee9d68f3 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/TaskerActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/TaskerActivity.kt @@ -14,10 +14,12 @@ import android.view.MenuItem import com.google.zxing.WriterException import com.tencent.mmkv.MMKV import com.v2ray.ang.AppConfig +import com.v2ray.ang.databinding.ActivityTaskerBinding import com.v2ray.ang.util.MmkvManager -import kotlinx.android.synthetic.main.activity_tasker.* class TaskerActivity : BaseActivity() { + private lateinit var binding: ActivityTaskerBinding + private var listview: ListView? = null private var lstData: ArrayList = ArrayList() private var lstGuid: ArrayList = ArrayList() @@ -26,7 +28,9 @@ class TaskerActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_tasker) + binding = ActivityTaskerBinding.inflate(layoutInflater) + val view = binding.root + setContentView(view) //add def value lstData.add("Default") @@ -55,7 +59,7 @@ class TaskerActivity : BaseActivity() { if (switch == null || TextUtils.isEmpty(guid)) { return } else { - switch_start_service.isChecked = switch + binding.switchStartService.isChecked = switch val pos = lstGuid.indexOf(guid.toString()) if (pos >= 0) { listview?.setItemChecked(pos, true) @@ -74,12 +78,12 @@ class TaskerActivity : BaseActivity() { } val extraBundle = Bundle() - extraBundle.putBoolean(AppConfig.TASKER_EXTRA_BUNDLE_SWITCH, switch_start_service.isChecked) + extraBundle.putBoolean(AppConfig.TASKER_EXTRA_BUNDLE_SWITCH, binding.switchStartService.isChecked) extraBundle.putString(AppConfig.TASKER_EXTRA_BUNDLE_GUID, lstGuid[position]) val intent = Intent() val remarks = lstData[position] - val blurb = if (switch_start_service.isChecked) { + val blurb = if (binding.switchStartService.isChecked) { "Start $remarks" } else { "Stop $remarks"