From 193df9962409eef55cb9906c703f069eb6deecd2 Mon Sep 17 00:00:00 2001 From: Finn M Glas Date: Tue, 16 Jun 2020 08:42:34 +0200 Subject: [PATCH] Create recycler with other internal actions Internal activities (settings or the apps list) can be opened through swiping or buttonpresses. Closes #33 --- .../com/finnmglas/launcher/MainActivity.kt | 31 ++++----- .../launcher/choose/ChooseFragmentOther.kt | 13 ++++ .../launcher/choose/other/OtherInfo.kt | 6 ++ .../choose/other/OtherRecyclerAdapter.kt | 65 +++++++++++++++++++ .../finnmglas/launcher/extern/Functions.kt | 27 +++++++- .../launcher/settings/SettingsFragmentApps.kt | 2 - .../main/res/layout/fragment_choose_other.xml | 14 ++++ .../main/res/layout/recycler_other_row.xml | 41 ++++++++++++ 8 files changed, 177 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/com/finnmglas/launcher/choose/other/OtherInfo.kt create mode 100644 app/src/main/java/com/finnmglas/launcher/choose/other/OtherRecyclerAdapter.kt create mode 100644 app/src/main/res/layout/recycler_other_row.xml diff --git a/app/src/main/java/com/finnmglas/launcher/MainActivity.kt b/app/src/main/java/com/finnmglas/launcher/MainActivity.kt index 44edd15..3d9c0e7 100644 --- a/app/src/main/java/com/finnmglas/launcher/MainActivity.kt +++ b/app/src/main/java/com/finnmglas/launcher/MainActivity.kt @@ -79,7 +79,7 @@ class MainActivity : AppCompatActivity(), showSettingsIcon() // As older APIs somehow do not recognize the xml defined onClick - activity_main_settings_icon.setOnClickListener() { openSettings() } + activity_main_settings_icon.setOnClickListener() { openSettings(this) } // Load apps list first - speed up settings that way AsyncTask.execute { viewAdapter = AppsRecyclerAdapter( this, "", "") } @@ -139,22 +139,17 @@ class MainActivity : AppCompatActivity(), clockTimer.cancel() } - private fun openSettings(){ - startActivity(Intent(this, SettingsActivity::class.java)) - overridePendingTransition(R.anim.bottom_up, android.R.anim.fade_out) - } - /** Touch- and Key-related functions to start activities */ override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { if (keyCode == KeyEvent.KEYCODE_BACK) { if (settingsIconShown) hideSettingsIcon() } - else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) launchApp(volumeUpApp, this) - else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) launchApp(volumeDownApp, this) + else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) launch(volumeUpApp, this) + else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) launch(volumeDownApp, this) return true } - fun dateViewOnTouch(v: View) { launchApp(calendarApp, this) } - fun timeViewOnTouch(v: View) { launchApp(clockApp, this) } + fun dateViewOnTouch(v: View) { launch(calendarApp, this) } + fun timeViewOnTouch(v: View) { launch(clockApp, this) } override fun onFling(e1: MotionEvent, e2: MotionEvent, dX: Float, dY: Float): Boolean { @@ -167,21 +162,21 @@ class MainActivity : AppCompatActivity(), val strictness = 4 // how distinguished the swipe has to be to be accepted // Only open if the swipe was not from the phones top edge - if (diffY < -height / 8 && abs(diffY) > strictness * abs(diffX) && e1.y > 100) launchApp(downApp, this) - else if (diffY > height / 8 && abs(diffY) > strictness * abs(diffX)) launchApp(upApp, this) - else if (diffX > width / 4 && abs(diffX) > strictness * abs(diffY)) launchApp(leftApp, this) - else if (diffX < -width / 4 && abs(diffX) > strictness * abs(diffY)) launchApp(rightApp, this) + if (diffY < -height / 8 && abs(diffY) > strictness * abs(diffX) && e1.y > 100) launch(downApp, this) + else if (diffY > height / 8 && abs(diffY) > strictness * abs(diffX)) launch(upApp, this) + else if (diffX > width / 4 && abs(diffX) > strictness * abs(diffY)) launch(leftApp, this) + else if (diffX < -width / 4 && abs(diffX) > strictness * abs(diffY)) launch(rightApp, this) return true } override fun onLongPress(event: MotionEvent) { - if(longClickApp != "") launchApp(longClickApp, this) - else openSettings() + if(longClickApp != "") launch(longClickApp, this) + else openSettings(this) } override fun onDoubleTap(event: MotionEvent): Boolean { - launchApp(doubleClickApp, this) + launch(doubleClickApp, this) return false } @@ -213,7 +208,7 @@ class MainActivity : AppCompatActivity(), settingsIconShown = false } - fun settingsIconOnTouch(view: View){ openSettings() } + fun settingsIconOnTouch(view: View){ openSettings(this) } override fun onTouchEvent(event: MotionEvent): Boolean { return if (mDetector.onTouchEvent(event)) { false } else { super.onTouchEvent(event) } diff --git a/app/src/main/java/com/finnmglas/launcher/choose/ChooseFragmentOther.kt b/app/src/main/java/com/finnmglas/launcher/choose/ChooseFragmentOther.kt index 62c2f43..d24315f 100644 --- a/app/src/main/java/com/finnmglas/launcher/choose/ChooseFragmentOther.kt +++ b/app/src/main/java/com/finnmglas/launcher/choose/ChooseFragmentOther.kt @@ -5,7 +5,9 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment +import androidx.recyclerview.widget.LinearLayoutManager import com.finnmglas.launcher.R +import com.finnmglas.launcher.choose.other.OtherRecyclerAdapter import com.finnmglas.launcher.extern.dominantColor import com.finnmglas.launcher.extern.getSavedTheme import kotlinx.android.synthetic.main.fragment_choose_other.* @@ -28,6 +30,17 @@ class ChooseFragmentOther : Fragment() { fragment_choose_other_container.setBackgroundColor(dominantColor) } + // set up the list / recycler + val viewManager = LinearLayoutManager(context) + val viewAdapter = OtherRecyclerAdapter(activity!!) + + fragment_choose_other_recycler_view.apply { + // improve performance (since content changes don't change the layout size) + setHasFixedSize(true) + layoutManager = viewManager + adapter = viewAdapter + } + super.onStart() } } \ No newline at end of file diff --git a/app/src/main/java/com/finnmglas/launcher/choose/other/OtherInfo.kt b/app/src/main/java/com/finnmglas/launcher/choose/other/OtherInfo.kt new file mode 100644 index 0000000..7d39b23 --- /dev/null +++ b/app/src/main/java/com/finnmglas/launcher/choose/other/OtherInfo.kt @@ -0,0 +1,6 @@ +package com.finnmglas.launcher.choose.other + +class OtherInfo(label: String, data: String) { + var label: CharSequence? = label + var data: CharSequence? = data +} \ No newline at end of file diff --git a/app/src/main/java/com/finnmglas/launcher/choose/other/OtherRecyclerAdapter.kt b/app/src/main/java/com/finnmglas/launcher/choose/other/OtherRecyclerAdapter.kt new file mode 100644 index 0000000..bfec1ab --- /dev/null +++ b/app/src/main/java/com/finnmglas/launcher/choose/other/OtherRecyclerAdapter.kt @@ -0,0 +1,65 @@ +package com.finnmglas.launcher.choose.other + +import android.app.Activity +import android.content.Context +import android.content.Intent +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView +import com.finnmglas.launcher.R +import com.finnmglas.launcher.extern.* +import com.finnmglas.launcher.forApp + +/* Will only be used if an app / action is picked */ +class OtherRecyclerAdapter(val activity: Activity): + RecyclerView.Adapter() { + + private val othersList: MutableList + + inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), + View.OnClickListener { + var textView: TextView = itemView.findViewById(R.id.row_other_name) + + + override fun onClick(v: View) { + val pos = adapterPosition + val content = othersList[pos] + + returnChoiceIntent(forApp, content.data.toString()) + } + + init { itemView.setOnClickListener(this) } + } + + override fun onBindViewHolder(viewHolder: ViewHolder, i: Int) { + val otherLabel = othersList[i].label.toString() + val otherData = othersList[i].data.toString() + + viewHolder.textView.text = otherLabel + } + + override fun getItemCount(): Int { return othersList.size } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + val inflater = LayoutInflater.from(parent.context) + val view: View = inflater.inflate(R.layout.recycler_other_row, parent, false) + return ViewHolder(view) + } + + init { + othersList = ArrayList() + othersList.add(OtherInfo("Settings", "launcher:settings")) + othersList.add(OtherInfo("AppsList", "launcher:choose")) + } + + /* */ + private fun returnChoiceIntent(forAction: String, value: String) { + val returnIntent = Intent() + returnIntent.putExtra("value", value) + returnIntent.putExtra("forApp", forApp) + activity.setResult(REQUEST_CHOOSE_APP, returnIntent) + activity.finish() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/finnmglas/launcher/extern/Functions.kt b/app/src/main/java/com/finnmglas/launcher/extern/Functions.kt index f32759d..d3a90d8 100644 --- a/app/src/main/java/com/finnmglas/launcher/extern/Functions.kt +++ b/app/src/main/java/com/finnmglas/launcher/extern/Functions.kt @@ -17,7 +17,10 @@ import android.view.animation.* import android.widget.Button import android.widget.ImageView import android.widget.Toast +import androidx.core.content.ContextCompat.startActivity +import com.finnmglas.launcher.ChooseActivity import com.finnmglas.launcher.R +import com.finnmglas.launcher.SettingsActivity import kotlin.math.roundToInt @@ -134,9 +137,19 @@ private fun getIntent(packageName: String, context: Context): Intent? { return intent } +// select what to launch +fun launch(data: String, activity: Activity) { + if (data.startsWith("launcher:")) // [type]:[info] + when(data.split(":")[1]) { + "settings" -> openSettings(activity) + "choose" -> openAppsList(activity) + } + else launchApp(data, activity) // app + +} + fun launchApp(packageName: String, context: Context) { - val intent = - getIntent(packageName, context) + val intent = getIntent(packageName, context) if (intent != null) { context.startActivity(intent) @@ -203,6 +216,16 @@ fun openAppSettings(pkg :String, context:Context){ context.startActivity(intent) } +fun openSettings(activity: Activity){ + activity.startActivity(Intent(activity, SettingsActivity::class.java)) + activity.overridePendingTransition(R.anim.bottom_up, android.R.anim.fade_out) +} + +fun openAppsList(activity: Activity){ + activity.startActivity(Intent(activity, ChooseActivity::class.java)) + activity.overridePendingTransition(R.anim.bottom_up, android.R.anim.fade_out) +} + fun loadSettings(sharedPref : SharedPreferences){ upApp = sharedPref.getString("action_upApp", "").toString() downApp = sharedPref.getString("action_downApp", "").toString() diff --git a/app/src/main/java/com/finnmglas/launcher/settings/SettingsFragmentApps.kt b/app/src/main/java/com/finnmglas/launcher/settings/SettingsFragmentApps.kt index b1914d8..11ff956 100644 --- a/app/src/main/java/com/finnmglas/launcher/settings/SettingsFragmentApps.kt +++ b/app/src/main/java/com/finnmglas/launcher/settings/SettingsFragmentApps.kt @@ -1,9 +1,7 @@ package com.finnmglas.launcher.settings import android.content.ActivityNotFoundException -import android.content.Context import android.content.Intent -import android.content.SharedPreferences import android.net.Uri import android.os.Bundle import android.view.LayoutInflater diff --git a/app/src/main/res/layout/fragment_choose_other.xml b/app/src/main/res/layout/fragment_choose_other.xml index 56f5609..b71efe3 100644 --- a/app/src/main/res/layout/fragment_choose_other.xml +++ b/app/src/main/res/layout/fragment_choose_other.xml @@ -14,6 +14,20 @@ android:layout_width="match_parent" android:layout_height="match_parent"> + + \ No newline at end of file diff --git a/app/src/main/res/layout/recycler_other_row.xml b/app/src/main/res/layout/recycler_other_row.xml new file mode 100644 index 0000000..ced4d3f --- /dev/null +++ b/app/src/main/res/layout/recycler_other_row.xml @@ -0,0 +1,41 @@ + + + + + +