Clean up unnecessary code and files

Also move the code to the right places (Functions.kt created)
This commit is contained in:
Finn M Glas 2020-05-19 09:08:16 +02:00
parent 9f48a13d33
commit 49baed4d65
No known key found for this signature in database
GPG key ID: 25037A2E81AB459C
8 changed files with 81 additions and 216 deletions

View file

@ -1,33 +0,0 @@
package com.finnmglas.launcher;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class FontAwesome extends androidx.appcompat.widget.AppCompatTextView {
public FontAwesome(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public FontAwesome(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FontAwesome(Context context) {
super(context);
init();
}
private void init() {
//Font name should not contain "/".
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/fa-solid-900.ttf");
setTypeface(tf);
}
}

View file

@ -1,32 +0,0 @@
package com.finnmglas.launcher;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
public class FontAwesomeBrand extends androidx.appcompat.widget.AppCompatTextView {
public FontAwesomeBrand(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public FontAwesomeBrand(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FontAwesomeBrand(Context context) {
super(context);
init();
}
private void init() {
//Font name should not contain "/".
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/fa-brand-400.ttf");
setTypeface(tf);
}
}

View file

@ -1,14 +1,16 @@
package com.finnmglas.launcher package com.finnmglas.launcher
import android.app.AlertDialog
import android.content.Context import android.content.Context
import android.content.DialogInterface
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences import android.content.SharedPreferences
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.net.Uri import android.net.Uri
import android.provider.Settings import android.provider.Settings
import androidx.core.content.ContextCompat.startActivity import android.widget.Toast
val none_msg = "None found" /** Activity related */
fun isInstalled(uri: String, context: Context): Boolean { fun isInstalled(uri: String, context: Context): Boolean {
try { try {
@ -19,6 +21,39 @@ fun isInstalled(uri: String, context: Context): Boolean {
return false return false
} }
private fun getIntent(packageName: String, context: Context): Intent? {
val intent: Intent? = context.packageManager.getLaunchIntentForPackage(packageName)
intent?.addCategory(Intent.CATEGORY_LAUNCHER)
return intent
}
fun launchApp(packageName: String, context: Context) {
val intent1 = getIntent(packageName, context)
if (intent1 != null) {
context.startActivity(intent1)
//overridePendingTransition(0, 0)
} else {
if (isInstalled(packageName, context)){
AlertDialog.Builder(context)
.setTitle("Can't open app")
.setMessage("Want to change its settings ('add it to the apps screen')?")
.setPositiveButton(android.R.string.yes,
DialogInterface.OnClickListener { dialog, which ->
openAppSettings(packageName, context)
})
.setNegativeButton(android.R.string.no, null)
.setIcon(android.R.drawable.ic_dialog_info)
.show()
} else {
Toast.makeText( context, "Open settings to choose an app for this action", Toast.LENGTH_SHORT).show()
}
}
}
/** Settings related */
fun openAppSettings(pkg :String, context:Context){ fun openAppSettings(pkg :String, context:Context){
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
intent.data = Uri.parse("package:$pkg") intent.data = Uri.parse("package:$pkg")
@ -37,7 +72,6 @@ fun loadSettings(sharedPref : SharedPreferences){
clockApp = sharedPref.getString("action_clockApp", "").toString() clockApp = sharedPref.getString("action_clockApp", "").toString()
} }
// Default settings are set here.
fun resetSettings(sharedPref : SharedPreferences, context: Context) : MutableList<String>{ fun resetSettings(sharedPref : SharedPreferences, context: Context) : MutableList<String>{
val defaultList :MutableList<String> = mutableListOf<String>() val defaultList :MutableList<String> = mutableListOf<String>()
@ -86,7 +120,7 @@ fun pickDefaultUpApp(context :Context) : Pair<String, String>{
else if(isInstalled("com.sec.android.app.sbrowser", context)) else if(isInstalled("com.sec.android.app.sbrowser", context))
return Pair("Samsung Internet", "com.sec.android.app.sbrowser") return Pair("Samsung Internet", "com.sec.android.app.sbrowser")
else else
return Pair("None, as we were unable to find one.", "") return Pair(context.getString(R.string.none_found), "")
} }
// Default downApps are Internal Search Apps // Default downApps are Internal Search Apps
@ -96,7 +130,7 @@ fun pickDefaultDownApp(context :Context) : Pair<String, String>{
else if(isInstalled("com.prometheusinteractive.voice_launcher", context)) else if(isInstalled("com.prometheusinteractive.voice_launcher", context))
return Pair("VoiceSearch", "com.prometheusinteractive.voice_launcher") return Pair("VoiceSearch", "com.prometheusinteractive.voice_launcher")
else else
return Pair(none_msg, "") return Pair(context.getString(R.string.none_found), "")
} }
// Default rightApps are Mailing Applications // Default rightApps are Mailing Applications
@ -108,7 +142,7 @@ fun pickDefaultRightApp(context :Context) : Pair<String, String>{
else if(isInstalled("com.google.android.gm", context)) else if(isInstalled("com.google.android.gm", context))
return Pair("Google Mail", "com.google.android.gm") return Pair("Google Mail", "com.google.android.gm")
else else
return Pair(none_msg, "") return Pair(context.getString(R.string.none_found), "")
} }
// Default leftApps are Calendar Applications // Default leftApps are Calendar Applications
@ -118,7 +152,7 @@ fun pickDefaultLeftApp(context :Context) : Pair<String, String>{
else if(isInstalled("com.samsung.android.calendar", context)) else if(isInstalled("com.samsung.android.calendar", context))
return Pair("Samsung Calendar", "com.samsung.android.calendar") return Pair("Samsung Calendar", "com.samsung.android.calendar")
else else
return Pair(none_msg, "") return Pair(context.getString(R.string.none_found), "")
} }
// Default volumeUpApps are Messengers // Default volumeUpApps are Messengers
@ -138,7 +172,7 @@ fun pickDefaultVolumeUpApp(context: Context) : Pair<String, String>{
else if(isInstalled("com.samsung.android.messaging", context)) else if(isInstalled("com.samsung.android.messaging", context))
return Pair("Samsung SMS", "com.samsung.android.messaging") return Pair("Samsung SMS", "com.samsung.android.messaging")
else else
return Pair(none_msg, "") return Pair(context.getString(R.string.none_found), "")
} }
// Default volumeDownApps are Utilities // Default volumeDownApps are Utilities
@ -150,5 +184,5 @@ fun pickDefaultVolumeDownApp(context: Context) : Pair<String, String>{
else if(isInstalled("com.sec.android.app.popupcalculator", context)) else if(isInstalled("com.sec.android.app.popupcalculator", context))
return Pair("Calculator", "com.sec.android.app.popupcalculator") return Pair("Calculator", "com.sec.android.app.popupcalculator")
else else
return Pair(none_msg, "") return Pair(context.getString(R.string.none_found), "")
} }

View file

@ -1,10 +1,6 @@
package com.finnmglas.launcher package com.finnmglas.launcher
import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.ActivityNotFoundException
import android.content.Context import android.content.Context
import android.content.DialogInterface
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.util.DisplayMetrics import android.util.DisplayMetrics
@ -18,7 +14,7 @@ import java.util.*
import kotlin.concurrent.fixedRateTimer import kotlin.concurrent.fixedRateTimer
import kotlin.math.abs import kotlin.math.abs
// App Launch Actions /** Variables for all of the app */
var upApp = "" var upApp = ""
var downApp = "" var downApp = ""
var rightApp = "" var rightApp = ""
@ -30,90 +26,16 @@ var calendarApp = ""
var clockApp = "" var clockApp = ""
class MainActivity : AppCompatActivity(), class MainActivity : AppCompatActivity(),
GestureDetector.OnGestureListener, GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
GestureDetector.OnDoubleTapListener {
/** Variables for this activity */
private lateinit var mDetector: GestureDetectorCompat private lateinit var mDetector: GestureDetectorCompat
// get device dimensions // get device dimensions
private val displayMetrics = DisplayMetrics() private val displayMetrics = DisplayMetrics()
private var clockTimer = Timer();
private fun getIntent(packageName: String): Intent? { /** Activity Lifecycle functions */
val intent: Intent? = packageManager.getLaunchIntentForPackage(packageName)
intent?.addCategory(Intent.CATEGORY_LAUNCHER)
return intent
}
private fun launchApp(packageName: String) {
val intent1 = getIntent(packageName)
if (intent1 != null) {
applicationContext.startActivity(intent1)
overridePendingTransition(0, 0)
} else {
if (isInstalled(packageName, this)){
AlertDialog.Builder(this)
.setTitle("Can't open app")
.setMessage("Want to change its settings ('add it to the apps screen')?")
.setPositiveButton(android.R.string.yes,
DialogInterface.OnClickListener { dialog, which ->
openAppSettings(packageName, this)
})
.setNegativeButton(android.R.string.no, null)
.setIcon(android.R.drawable.ic_dialog_info)
.show()
} else {
Toast.makeText(
this,
"Open settings to choose an app for this action",
Toast.LENGTH_SHORT
).show()
}
}
}
fun launchCalendar(v: View) {
launchApp(calendarApp)
}
fun launchClock(v: View) {
launchApp(clockApp)
}
fun launchUpApp() {
launchApp(upApp)
}
fun launchDownApp() {
launchApp(downApp)
}
fun lauchLeftApp() {
launchApp(leftApp)
}
fun lauchRightApp() {
launchApp(rightApp)
}
fun lauchVolumeUpApp() {
launchApp(volumeUpApp)
}
fun lauchVolumeDownApp() {
launchApp(volumeDownApp)
}
/* Overrides */
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KeyEvent.KEYCODE_BACK) return true
else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) lauchVolumeUpApp()
else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) lauchVolumeDownApp()
return true
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -135,7 +57,6 @@ GestureDetector.OnDoubleTapListener {
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
} }
// After the app is created or when it restarts (inactivity) - Fixes Issue #9
override fun onStart(){ override fun onStart(){
super.onStart() super.onStart()
@ -148,7 +69,7 @@ GestureDetector.OnDoubleTapListener {
val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
val timeFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault()) val timeFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
fixedRateTimer("timer", false, 0L, 1000) { clockTimer = fixedRateTimer("timer", true, 0L, 1000) {
this@MainActivity.runOnUiThread { this@MainActivity.runOnUiThread {
dateView.text = dateFormat.format(Date()) dateView.text = dateFormat.format(Date())
timeView.text = timeFormat.format(Date()) timeView.text = timeFormat.format(Date())
@ -159,24 +80,19 @@ GestureDetector.OnDoubleTapListener {
mDetector.setOnDoubleTapListener(this) mDetector.setOnDoubleTapListener(this)
} }
override fun onTouchEvent(event: MotionEvent): Boolean { /** Touch- and Key-related functions to start activities */
return if (mDetector.onTouchEvent(event)) {
true
} else {
super.onTouchEvent(event)
}
}
override fun onDown(event: MotionEvent): Boolean { override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KeyEvent.KEYCODE_BACK) return true
else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) launchApp(volumeUpApp, this)
else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) launchApp(volumeDownApp, this)
return true return true
} }
override fun onFling( fun dateViewOnTouch(v: View) { launchApp(calendarApp, this) }
e1: MotionEvent, fun timeViewOnTouch(v: View) { launchApp(clockApp, this) }
e2: MotionEvent,
differenceX: Float, override fun onFling(e1: MotionEvent, e2: MotionEvent, dX: Float, dY: Float): Boolean {
differenceY: Float
): Boolean {
windowManager.defaultDisplay.getMetrics(displayMetrics) windowManager.defaultDisplay.getMetrics(displayMetrics)
val width = displayMetrics.widthPixels val width = displayMetrics.widthPixels
@ -185,55 +101,33 @@ GestureDetector.OnDoubleTapListener {
val diffX = e1.x - e2.x val diffX = e1.x - e2.x
val diffY = e1.y - e2.y val diffY = e1.y - e2.y
val strictness = 4 // of direction val strictness = 4 // how distinguished the swipe has to be to be accepted
/* Decide for an action */ // 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)
if (diffY > height / 8 && abs(diffY) > strictness * abs(diffX)) launchUpApp() else if (diffY > height / 8 && abs(diffY) > strictness * abs(diffX)) launchApp(upApp, this)
// Only open if the swipe was not from the phone edge else if (diffX > width / 4 && abs(diffX) > strictness * abs(diffY)) launchApp(leftApp, this)
else if (diffY < -height / 8 && abs(diffY) > strictness * abs(diffX) && e1.y > 100) launchDownApp() else if (diffX < -width / 4 && abs(diffX) > strictness * abs(diffY)) launchApp(rightApp, this)
else if (diffX > width / 4 && abs(diffX) > strictness * abs(diffY)) lauchLeftApp()
else if (diffX < -width / 4 && abs(diffX) > strictness * abs(diffY)) lauchRightApp()
return true return true
} }
// Open Settings // Open Settings Activity
override fun onLongPress(event: MotionEvent) { override fun onLongPress(event: MotionEvent) {
startActivity(Intent(this, SettingsActivity::class.java)) startActivity(Intent(this, SettingsActivity::class.java))
} }
override fun onScroll( override fun onTouchEvent(event: MotionEvent): Boolean {
e1: MotionEvent, return if (mDetector.onTouchEvent(event)) { true } else { super.onTouchEvent(event) }
e2: MotionEvent,
diffX: Float,
diffY: Float
): Boolean {
return true
}
override fun onShowPress(event: MotionEvent) {
}
override fun onSingleTapUp(event: MotionEvent): Boolean {
return true
}
override fun onDoubleTap(event: MotionEvent): Boolean {
return true
}
override fun onDoubleTapEvent(event: MotionEvent): Boolean {
return true
}
override fun onSingleTapConfirmed(event: MotionEvent): Boolean {
return true
} }
/* TODO: Remove those. For now they are necessary
* because this inherits from GestureDetector.OnGestureListener */
override fun onDoubleTap(event: MotionEvent): Boolean { return true }
override fun onDoubleTapEvent(event: MotionEvent): Boolean { return true }
override fun onDown(event: MotionEvent): Boolean { return true }
override fun onScroll(e1: MotionEvent, e2: MotionEvent, dX: Float, dY: Float): Boolean { return true }
override fun onShowPress(event: MotionEvent) {}
override fun onSingleTapUp(event: MotionEvent): Boolean { return true }
override fun onSingleTapConfirmed(event: MotionEvent): Boolean { return true }
} }

View file

@ -13,7 +13,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="left|center_vertical" android:gravity="left|center_vertical"
android:onClick="launchCalendar" android:onClick="dateViewOnTouch"
android:textColor="#ccc" android:textColor="#ccc"
android:textSize="30sp" android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
@ -27,7 +27,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="left|center_vertical" android:gravity="left|center_vertical"
android:onClick="launchClock" android:onClick="timeViewOnTouch"
android:textColor="#ccc" android:textColor="#ccc"
android:textSize="18sp" android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"

View file

@ -1,4 +1,6 @@
<resources> <resources>
<string name="app_name">Launcher</string> <string name="app_name">Launcher</string>
<string name="preference_file_key">V3RYR4ND0MK3YCR4P</string> <string name="preference_file_key">V3RYR4ND0MK3YCR4P</string>
<string name="none_found">None found</string>
</resources> </resources>