init_main_activity

This commit is contained in:
LUX-Timber 2024-04-03 10:37:24 +08:00
parent 7fca0dc814
commit 77133dd6b5
16 changed files with 523 additions and 23 deletions

1
.gitignore vendored
View File

@ -13,3 +13,4 @@
.externalNativeBuild
.cxx
local.properties
/app/src/main/assets/keyboard.json

View File

@ -30,6 +30,11 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding = true
}
}
dependencies {
@ -42,4 +47,7 @@ dependencies {
testImplementation libs.junit
androidTestImplementation libs.androidx.junit
androidTestImplementation libs.androidx.espresso.core
implementation 'com.google.code.gson:gson:2.10.1'
}

View File

@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"

View File

@ -2,19 +2,29 @@ package com.timber.soft.newkeyboard
import android.inputmethodservice.InputMethodService
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener
import android.view.View
class AppInputMethodService :InputMethodService(),OnKeyboardActionListener{
class AppInputMethodService : InputMethodService(), OnKeyboardActionListener {
/**
* 向用户展示
* 构建键盘视图
*/
override fun onCreateInputView(): View {
return super.onCreateInputView()
TODO("Not yet implemented")
}
/**
* 向用户展示键盘时候调用
*/
override fun onWindowShown() {
super.onWindowShown()
TODO("Not yet implemented")
}
/**
* Called when the user presses a key.
*
@ -33,6 +43,7 @@ class AppInputMethodService :InputMethodService(),OnKeyboardActionListener{
TODO("Not yet implemented")
}
override fun onText(text: CharSequence?) {
}

View File

@ -1,21 +1,196 @@
package com.timber.soft.newkeyboard.activity
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.graphics.Color
import android.graphics.Typeface
import android.net.ConnectivityManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import android.util.Log
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.content.ContextCompat
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.google.android.material.tabs.TabLayout
import com.timber.soft.newkeyboard.R
import com.timber.soft.newkeyboard.databinding.ActivityMainBinding
import com.timber.soft.newkeyboard.fragment.VPFragment
import com.timber.soft.newkeyboard.model.JsonDeserializer.parseJsonFromAssets
import com.timber.soft.newkeyboard.model.RootModel
import com.timber.soft.newkeyboard.tools.StatusBarTools.dpCovertPx
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var fragmentList: ArrayList<Fragment>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
// 获取ConnectivityManager实例
val connectivityManager = getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
// 检查网络连接状态
val networkInfo = connectivityManager.activeNetworkInfo
if (networkInfo != null && networkInfo.isConnected) {
// 已连接到互联网
Log.d("NetworkStatus", "Connected to the Internet")
} else {
// 未连接到互联网
Log.d("NetworkStatus", "Not connected to the Internet")
}
// 设置Padding上边距留出沉浸式状态栏空间
binding.root.setPadding(0, dpCovertPx(this), 0, 0)
// 设置沉浸式状态栏
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.decorView.systemUiVisibility =
(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE) or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
window.statusBarColor = Color.TRANSPARENT
}
initDrawer()
val rootModelList: MutableList<RootModel> = mutableListOf()
val result = parseJsonFromAssets(this@MainActivity, "keyboard.json")
if (result != null) {
rootModelList.addAll(result)
}
rootModelList.shuffle()
for (i in rootModelList) {
binding.tabLayout.addTab(
binding.tabLayout.newTab().setCustomView(R.layout.item_tab)
)
}
fragmentList = arrayListOf()
for (i in 0 until binding.tabLayout.tabCount) {
val tabView = binding.tabLayout.getTabAt(i)?.customView
if (tabView != null) {
val rootModel = rootModelList[i]
val textName = tabView.findViewById<TextView>(R.id.keyboard_kind_name)
textName.text = rootModel.className
fragmentList.add(VPFragment(rootModel))
}
}
binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(p0: TabLayout.Tab?) {
setTabSize(p0)
}
override fun onTabUnselected(p0: TabLayout.Tab?) {
p0?.customView = null
}
override fun onTabReselected(p0: TabLayout.Tab?) {
// null
}
})
binding.viewpager.offscreenPageLimit = 3
binding.viewpager.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
override fun getCount(): Int {
return fragmentList.size
}
override fun getItem(position: Int): Fragment {
return fragmentList[position]
}
override fun getPageTitle(position: Int): CharSequence? {
return rootModelList[position].className
}
}
binding.tabLayout.setupWithViewPager(binding.viewpager)
}
private fun setTabSize(p0: TabLayout.Tab?) {
val textView = TextView(this)
//字体样式
val selectedSize =
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 22f, resources.displayMetrics)
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, selectedSize)
textView.typeface = Typeface.defaultFromStyle(Typeface.BOLD) //加粗
textView.gravity = Gravity.CENTER
//选中的字体颜色
textView.setTextColor(ContextCompat.getColor(this, R.color.theme_color))
textView.text = p0!!.text
p0.customView = textView
}
private fun initDrawer() {
binding.layoutRate.setOnClickListener() {
val url = getString(R.string.share_link) + packageName
// 创建intent打开链接
val intent = Intent(Intent.ACTION_VIEW)
intent.setData(Uri.parse(url))
startActivity(intent)
}
// 绑定抽屉中的分享按钮
binding.layoutShare.setOnClickListener() {
// 商店中包的位置
val url = getString(R.string.share_link) + packageName
val intent = Intent(Intent.ACTION_SEND)
intent.setType("text/plain")
intent.putExtra(Intent.EXTRA_TEXT, url)
startActivity(intent)
}
// 绑定抽屉中的版本信息
val versionName = getVersionName()
binding.textAppVersion.text = versionName
// 打开抽屉
binding.imageMenu.setOnClickListener() {
binding.drawerParent.openDrawer(GravityCompat.START)
}
binding.drawerParent.addDrawerListener(object : DrawerLayout.DrawerListener {
override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
}
override fun onDrawerOpened(drawerView: View) {
// 设置监听事件防止 Drawer 穿透
drawerView.isClickable = true
}
override fun onDrawerClosed(drawerView: View) {
}
override fun onDrawerStateChanged(newState: Int) {
}
})
}
private fun getVersionName(): String {
val pInfo: PackageInfo
try {
pInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
packageManager.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0))
} else {
packageManager.getPackageInfo(packageName, 0)
}
} catch (e: PackageManager.NameNotFoundException) {
return ""
}
return "Version: " + pInfo.versionName
}
}

View File

@ -0,0 +1,9 @@
package com.timber.soft.newkeyboard.fragment
import androidx.fragment.app.Fragment
import com.timber.soft.newkeyboard.model.RootModel
class VPFragment(private val rootModel: RootModel) : Fragment() {
}

View File

@ -0,0 +1,44 @@
package com.timber.soft.newkeyboard.model
import android.content.Context
import com.google.gson.Gson
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.io.Serializable
import java.nio.charset.StandardCharsets
object JsonDeserializer {
fun parseJsonFromAssets(context: Context, fileName: String): List<RootModel>? {
var dataItems: List<RootModel>? = null
try {
val inputStream = context.assets.open(fileName)
val reader = BufferedReader(InputStreamReader(inputStream, StandardCharsets.UTF_8))
val stringBuilder = StringBuilder()
var line: String?
while (reader.readLine().also { line = it } != null) {
stringBuilder.append(line)
}
inputStream.close()
reader.close()
val gson = Gson()
val dataItemArray =
gson.fromJson(stringBuilder.toString(), Array<RootModel>::class.java)
dataItems = dataItemArray.toList()
} catch (e: IOException) {
e.printStackTrace()
}
return dataItems
}
}
data class RootModel(
val className: String, val list: List<DataModel>
) : Serializable
data class DataModel(
val preview: String, val thumb: String, val title: String, val zipUrl: String
) : Serializable

View File

@ -0,0 +1,15 @@
package com.timber.soft.newkeyboard.tools
import android.content.Context
object StatusBarTools {
fun dpCovertPx(context: Context): Int {
// 获取当前设备的屏幕密度,并赋值给变量 scale
var result = 0
val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
if (resourceId > 0) {
result = context.resources.getDimensionPixelSize(resourceId)
}
return result
}
}

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="64dp"
android:height="64dp"
android:viewportWidth="1024"
android:viewportHeight="1024">
<path
android:pathData="M708.3,669.9l213.3,-123.7c8.5,-4.3 17.1,-17.1 17.1,-25.6v-12.8c0,-12.8 -4.3,-21.3 -17.1,-25.6l-213.3,-123.7 -140.8,153.6 140.8,157.9zM533.3,473.6l128,-145.1L187.7,46.9c-8.5,-4.3 -17.1,-4.3 -25.6,0l371.2,426.7zM533.3,554.7L162.1,981.3c8.5,4.3 17.1,4.3 25.6,0l473.6,-277.3 -128,-149.3zM128,89.6v844.8l371.2,-422.4L128,89.6z"
android:fillColor="#2c2c2c"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="64dp"
android:height="64dp"
android:viewportWidth="1024"
android:viewportHeight="1024">
<path
android:pathData="M170.7,213.3h682.7v85.3L170.7,298.7L170.7,213.3zM170.7,725.3h682.7v85.3L170.7,810.7v-85.3zM170.7,469.3h682.7v85.3L170.7,554.7v-85.3z"
android:fillColor="#2c2c2c"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="64dp"
android:height="64dp"
android:viewportWidth="1024"
android:viewportHeight="1024">
<path
android:pathData="M879.5,470.3H244.8L507.1,209.5a41.7,41.7 0,0 0,-58.9 -58.9L115,482.7a41.9,41.9 0,0 0,0 58.9l333.3,333a41.7,41.7 0,0 0,58.9 0,41.9 41.9,0 0,0 0,-58.9L244.8,553.7h634.6a41.7,41.7 0,1 0,0 -83.3z"
android:fillColor="#2c2c2c"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="64dp"
android:height="64dp"
android:viewportWidth="1024"
android:viewportHeight="1024">
<path
android:pathData="M1009.2,5.1a32.1,32.1 0,0 0,-35.1 0.3l-959.7,639.8a31.8,31.8 0,0 0,-14 29.9,31.8 31.8,0 0,0 19.9,26.4l250.3,100.2 117.7,206a32,32 0,0 0,27.4 16.1L416,1023.7c11.2,0 21.7,-6 27.4,-15.5l66.4,-110.8 310.1,124a31.7,31.7 0,0 0,27.5 -1.9c8.4,-4.7 14.2,-13.1 15.9,-22.6l160,-959.7A32.1,32.1 0,0 0,1009.2 5.1zM100.4,664.7L841.8,170.4 302.8,747.4c-2.8,-1.7 -5.4,-3.9 -8.5,-5.2l-193.8,-77.5zM326.1,769.9l-0.2,-0.3L931.8,121 415.2,925.9l-89.2,-156zM806.8,947.2l-273.2,-109.3a63.6,63.6 0,0 0,-19.9 -3.8L934.4,181.9l-127.6,765.3z"
android:fillColor="#2c2c2c"/>
</vector>

View File

@ -1,19 +1,186 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_parent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--主视图-->
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/backgroundLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--菜单按钮-->
<ImageView
android:id="@+id/image_menu"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="7dp"
android:padding="10dp"
android:src="@drawable/svg_menu"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!--上方标题-->
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center"
android:text="@string/app_name"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!--tab-->
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_title"
app:tabIndicatorColor="@color/theme_color"
app:tabIndicatorFullWidth="false"
app:tabMode="scrollable" />
<!--内容-->
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/tabLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
<!--抽屉视图-->
<RelativeLayout
android:id="@+id/drawer_recycler_view"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/white">
<!--抽屉头部-->
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/topPanel"
android:layout_width="match_parent"
android:layout_height="160dp"
android:paddingBottom="14dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
<!--appLog-->
<androidx.cardview.widget.CardView
android:id="@+id/imageLogo"
android:layout_width="54dp"
android:layout_height="54dp"
android:layout_marginTop="40dp"
app:cardCornerRadius="12dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher" />
</androidx.cardview.widget.CardView>
<!--AppName-->
<TextView
android:id="@+id/text_app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/app_name"
android:textColor="@color/black"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/imageLogo" />
<!--Version-->
<TextView
android:id="@+id/text_app_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="@string/app_name"
android:textColor="@color/black"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="@id/text_app_name"
app:layout_constraintRight_toRightOf="@id/text_app_name"
app:layout_constraintTop_toBottomOf="@id/text_app_name" />
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:id="@+id/layoutRate"
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_below="@id/topPanel"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/im_rate"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_marginStart="25dp"
android:src="@drawable/svg_google_play" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:gravity="center"
android:text="@string/main_menu_rate"
android:textColor="@color/black"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/layoutShare"
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_below="@id/layoutRate"
android:layout_marginTop="16dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/im_share"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_marginStart="25dp"
android:src="@drawable/svg_share" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:gravity="center"
android:text="@string/main_menu_share"
android:textColor="@color/black"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
</androidx.drawerlayout.widget.DrawerLayout>
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/keyboard_kind_name"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:padding="10dp"
android:text="text_TAB"
android:textColor="@color/black"
android:textSize="14sp" />
<View
android:id="@+id/line_index"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/theme_color" />
</LinearLayout>

View File

@ -2,4 +2,9 @@
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="theme_color">#FF9800</color>
</resources>

View File

@ -1,3 +1,6 @@
<resources>
<string name="app_name">NewKeyboard</string>
<string name="main_menu_share">Share our Apps</string>
<string name="main_menu_rate">Find us in store</string>
<string name="share_link">https://play.google.com/store/apps/details?id=</string>
</resources>