diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt b/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt index d8f2d54..f40f969 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt @@ -4,6 +4,7 @@ import android.annotation.SuppressLint import android.content.Context import android.content.pm.LauncherActivityInfo import android.os.UserHandle +import android.view.Gravity import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -103,15 +104,33 @@ class AppMenuAdapter( override fun onBindViewHolder(holder: AppViewHolder, position: Int) { val app = apps[position] - holder.textView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null) - if (app.second.second != 0) { holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_work_app, null),null,null,null) } else { holder.textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(activity.resources, R.drawable.ic_empty, null),null,null,null) } - holder.textView.compoundDrawablePadding = 0 + + /* + 0 = left + 1 = center + 2 = right + */ + + when (sharedPreferenceManager.getAppMenuAlignment(activity)) { + 0 -> { + holder.textView.setCompoundDrawablesWithIntrinsicBounds(holder.textView.compoundDrawables.filterNotNull().first(),null, null, null) + holder.textView.gravity = Gravity.CENTER_VERTICAL or Gravity.START + } + 1 -> { + holder.textView.setCompoundDrawablesWithIntrinsicBounds(holder.textView.compoundDrawables.filterNotNull().first(),null,holder.textView.compoundDrawables.filterNotNull().first(), null) + holder.textView.gravity = Gravity.CENTER + } + 2 -> { + holder.textView.setCompoundDrawablesWithIntrinsicBounds(null,null, holder.textView.compoundDrawables.filterNotNull().first(), null) + holder.textView.gravity = Gravity.CENTER_VERTICAL or Gravity.END + } + } val appInfo = app.first.activityInfo.applicationInfo holder.textView.text = sharedPreferenceManager.getAppName(activity, app.first.applicationInfo.packageName,app.second.second, holder.itemView.context.packageManager.getApplicationLabel(appInfo)) @@ -119,10 +138,29 @@ class AppMenuAdapter( holder.textView.visibility = View.VISIBLE } + override fun onViewAttachedToWindow(holder: AppViewHolder) { + super.onViewAttachedToWindow(holder) + when (sharedPreferenceManager.getAppMenuAlignment(activity)) { + 0 -> { + holder.textView.setCompoundDrawablesWithIntrinsicBounds(holder.textView.compoundDrawables.filterNotNull().first(),null, null, null) + holder.textView.gravity = Gravity.CENTER_VERTICAL or Gravity.START + } + 1 -> { + holder.textView.setCompoundDrawablesWithIntrinsicBounds(holder.textView.compoundDrawables.filterNotNull().first(),null,holder.textView.compoundDrawables.filterNotNull().first(), null) + holder.textView.gravity = Gravity.CENTER + } + 2 -> { + holder.textView.setCompoundDrawablesWithIntrinsicBounds(null,null, holder.textView.compoundDrawables.filterNotNull().first(), null) + holder.textView.gravity = Gravity.CENTER_VERTICAL or Gravity.END + } + } + } + override fun getItemCount(): Int { return apps.size } + @SuppressLint("NotifyDataSetChanged") fun updateApps(newApps: List>>) { apps = newApps.toMutableList() notifyDataSetChanged() diff --git a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt index 5c5d98f..4dbba2f 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt @@ -5,7 +5,6 @@ import android.content.Context import android.content.Intent import android.content.pm.LauncherActivityInfo import android.content.pm.LauncherApps -import android.content.pm.PackageManager import android.os.Build import android.os.Bundle import android.os.Handler @@ -15,12 +14,12 @@ import android.provider.MediaStore import android.text.Editable import android.text.TextWatcher import android.view.GestureDetector +import android.view.Gravity import android.view.MotionEvent import android.view.View import android.view.inputmethod.InputMethodManager import android.widget.EditText import android.widget.LinearLayout -import android.widget.Spinner import android.widget.TextClock import android.widget.TextView import android.widget.Toast @@ -29,9 +28,8 @@ import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintSet -import androidx.core.app.ActivityCompat -import androidx.core.content.ContextCompat import androidx.core.content.res.ResourcesCompat +import androidx.core.view.children import androidx.core.view.marginLeft import androidx.recyclerview.widget.RecyclerView import eu.ottop.yamlauncher.databinding.ActivityMainBinding @@ -55,7 +53,7 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap private lateinit var recyclerView: RecyclerView private lateinit var searchView: EditText - private lateinit var adapter: AppMenuAdapter + private var adapter: AppMenuAdapter? = null private var job: Job? = null val cameraIntent = Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE) val phoneIntent = Intent(Intent.ACTION_DIAL) @@ -75,6 +73,8 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap private var clockMargin = 0 private lateinit var constraintLayout: ConstraintLayout + private lateinit var dateText: TextClock + @SuppressLint("ClickableViewAccessibility") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -95,7 +95,7 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap setupApps() - val dateText = findViewById(R.id.text_date) + dateText = findViewById(R.id.text_date) batteryReceiver = BatteryReceiver.register(this, dateText) @@ -129,18 +129,20 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap unregisterReceiver(batteryReceiver) } + @SuppressLint("NotifyDataSetChanged") override fun onStart() { super.onStart() startTask() // Keyboard is sometimes open when going back to the app, so close it. closeKeyboard() + setClockAlignment() + setShortCutAlignment() + + adapter?.notifyDataSetChanged() + } - override fun onResume() { - super.onResume() - setClockAlignment() - } open inner class GestureListener : GestureDetector.SimpleOnGestureListener() { @@ -235,7 +237,6 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap textView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_empty, null),null,null,null) - textView.compoundDrawablePadding = 0 unselectedListeners(textView) } @@ -255,8 +256,8 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap Toast.makeText(this, "Long click to select an app", Toast.LENGTH_SHORT).show() } textView.setOnLongClickListener { - adapter.menuMode = "shortcut" - adapter.shortcutTextView = textView + adapter?.menuMode = "shortcut" + adapter?.shortcutTextView = textView toAppMenu() return@setOnLongClickListener true @@ -382,7 +383,7 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap } fun openAppMenuActivity() { - adapter.menuMode = "app" + adapter?.menuMode = "app" binding.menutitle.visibility = View.GONE toAppMenu() } @@ -441,9 +442,10 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap if (userProfile != 0) { shortcutView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_work_app, null),null,null,null) } - else { + else if (shortcutView.gravity != Gravity.CENTER) { shortcutView.setCompoundDrawablesWithIntrinsicBounds(ResourcesCompat.getDrawable(resources, R.drawable.ic_empty, null),null,null,null) } + shortcutView.text = textView.text.toString() shortcutView.setOnClickListener { val mainActivity = launcherApps.getActivityList(appInfo.applicationInfo.packageName, userHandle).firstOrNull() @@ -510,11 +512,8 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap imm.hideSoftInputFromWindow(binding.root.windowToken, 0) } - - - @SuppressLint("NotifyDataSetChanged") private fun updateMenu(updatedApps : List>>) { - adapter.updateApps(updatedApps) + adapter?.updateApps(updatedApps) println("moved") } @@ -531,48 +530,85 @@ class MainActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, Ap } private fun setClockAlignment() { - val clockAlignment = sharedPreferenceManager.getClockAlignment(this@MainActivity) + setConstraintAlignment(sharedPreferenceManager.getClockAlignment(this@MainActivity), clock.id, clockMargin) + } + + private fun setConstraintAlignment(alignment: Int, widgetId: Int, margin: Int) { val constraintSet = ConstraintSet() constraintSet.clone(constraintLayout) - println(clockAlignment) + println(alignment) /* 0 = left 1 = center 2 = right */ - - if (clockAlignment == 2) { - constraintSet.clear(clock.id, ConstraintSet.START) + if (alignment == 2) { + constraintSet.clear(widgetId, ConstraintSet.START) } - else if (clockAlignment == 0) { - constraintSet.clear(clock.id, ConstraintSet.END) + else if (alignment == 0) { + constraintSet.clear(widgetId, ConstraintSet.END) } - if (clockAlignment == 1 || clockAlignment == 0) { + if (alignment == 1 || alignment == 0) { constraintSet.connect( - clock.id, + widgetId, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, - clockMargin + margin ) + } - if (clockAlignment != 0) { + if (alignment != 0) { constraintSet.connect( - clock.id, + widgetId, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, - clockMargin + margin ) } constraintSet.applyTo(constraintLayout) } + private fun setShortCutAlignment() { + setLinearAlignment(binding.homeView) + } + + private fun setLinearAlignment(shortcuts: LinearLayout) { + shortcuts.children.forEach { + + if (it is TextView) { + + /* + 0 = left + 1 = center + 2 = right + */ + + when (sharedPreferenceManager.getHomeAppAlignment(this@MainActivity)) { + 0 -> { + it.setCompoundDrawablesWithIntrinsicBounds(it.compoundDrawables.filterNotNull().first(),null, null, null) + it.gravity = Gravity.CENTER_VERTICAL or Gravity.START + } + 1 -> { + it.setCompoundDrawablesWithIntrinsicBounds(it.compoundDrawables.filterNotNull().first(),null,it.compoundDrawables.filterNotNull().first(), null) + it.gravity = Gravity.CENTER + } + 2 -> { + it.setCompoundDrawablesWithIntrinsicBounds(null,null, it.compoundDrawables.filterNotNull().first(), null) + it.gravity = Gravity.CENTER_VERTICAL or Gravity.END + } + } + + } + } + } + fun isJobActive(): Boolean { return if (job != null) { job!!.isActive diff --git a/app/src/main/java/eu/ottop/yamlauncher/SettingsActivity.kt b/app/src/main/java/eu/ottop/yamlauncher/SettingsActivity.kt index 4139221..a58fdae 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/SettingsActivity.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/SettingsActivity.kt @@ -22,7 +22,30 @@ class SettingsActivity : AppCompatActivity() { override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) { // Get the selected item sharedPreferenceManager.setClockAlignment(this@SettingsActivity, position) - println(position) + } + + override fun onNothingSelected(parent: AdapterView<*>) { + } + } + + binding.homeAppAlignment.setSelection(sharedPreferenceManager.getHomeAppAlignment(this@SettingsActivity)) + + binding.homeAppAlignment.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { + override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) { + // Get the selected item + sharedPreferenceManager.setHomeAppAlignment(this@SettingsActivity, position) + } + + override fun onNothingSelected(parent: AdapterView<*>) { + } + } + + binding.appAlignment.setSelection(sharedPreferenceManager.getAppMenuAlignment(this@SettingsActivity)) + + binding.appAlignment.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { + override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) { + // Get the selected item + sharedPreferenceManager.setAppMenuAlignment(this@SettingsActivity, position) } override fun onNothingSelected(parent: AdapterView<*>) { diff --git a/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt b/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt index 1b6c8f6..ae329d6 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt @@ -85,4 +85,30 @@ class SharedPreferenceManager { val key = "clock_alignment" return sharedPreferences.getInt(key, 0) } + + fun setHomeAppAlignment(cont: Context, alignment: Int) { + val editor = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE).edit() + val key = "home_app_alignment" + editor.putInt(key, alignment) + editor.apply() + } + + fun getHomeAppAlignment(cont: Context) : Int { + val sharedPreferences = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE) + val key = "home_app_alignment" + return sharedPreferences.getInt(key, 0) + } + + fun setAppMenuAlignment(cont: Context, alignment: Int) { + val editor = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE).edit() + val key = "app_menu_alignment" + editor.putInt(key, alignment) + editor.apply() + } + + fun getAppMenuAlignment(cont: Context) : Int { + val sharedPreferences = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE) + val key = "app_menu_alignment" + return sharedPreferences.getInt(key, 0) + } } \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_work_app.xml b/app/src/main/res/drawable/ic_work_app.xml index cb421b0..72c5ad7 100644 --- a/app/src/main/res/drawable/ic_work_app.xml +++ b/app/src/main/res/drawable/ic_work_app.xml @@ -7,11 +7,11 @@ android:alpha="1"> + - + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 140ae6c..4ac2768 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -113,6 +113,7 @@ android:textAppearance="@android:style/TextAppearance.DeviceDefault" android:textColor="#F3F3F3" android:textSize="20sp" + app:layout_constraintEnd_toEndOf="@+id/text_clock" app:layout_constraintStart_toStartOf="@+id/text_clock" app:layout_constraintTop_toBottomOf="@+id/text_clock" /> @@ -131,6 +132,7 @@ android:autoSizeMaxTextSize="28sp" android:autoSizeTextType="uniform" android:clickable="false" + android:drawablePadding="3dp" android:gravity="center_vertical" android:paddingLeft="20dp" android:paddingRight="20dp" @@ -152,6 +154,7 @@ android:autoSizeMaxTextSize="28sp" android:autoSizeTextType="uniform" android:clickable="false" + android:drawablePadding="3dp" android:gravity="center_vertical" android:paddingLeft="20dp" android:paddingRight="20dp" @@ -174,6 +177,7 @@ android:autoSizeMaxTextSize="28sp" android:autoSizeTextType="uniform" android:clickable="false" + android:drawablePadding="3dp" android:gravity="center_vertical" android:paddingLeft="20dp" android:paddingRight="20dp" @@ -196,6 +200,7 @@ android:autoSizeMaxTextSize="28sp" android:autoSizeTextType="uniform" android:clickable="false" + android:drawablePadding="3dp" android:gravity="center_vertical" android:paddingLeft="20dp" android:paddingRight="20dp" @@ -219,6 +224,7 @@ android:autoSizeMaxTextSize="28sp" android:autoSizeTextType="uniform" android:clickable="false" + android:drawablePadding="3dp" android:gravity="center_vertical" android:paddingLeft="20dp" android:paddingRight="20dp" @@ -242,6 +248,7 @@ android:autoSizeMaxTextSize="28sp" android:autoSizeTextType="uniform" android:clickable="false" + android:drawablePadding="3dp" android:gravity="center_vertical" android:paddingLeft="20dp" android:paddingRight="20dp" @@ -265,6 +272,7 @@ android:autoSizeMaxTextSize="28sp" android:autoSizeTextType="uniform" android:clickable="false" + android:drawablePadding="3dp" android:gravity="center_vertical" android:paddingLeft="20dp" android:paddingRight="20dp" @@ -288,6 +296,7 @@ android:autoSizeMaxTextSize="28sp" android:autoSizeTextType="uniform" android:clickable="false" + android:drawablePadding="3dp" android:gravity="center_vertical" android:paddingLeft="20dp" android:paddingRight="20dp" diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml index 6ed414c..8294764 100644 --- a/app/src/main/res/layout/activity_settings.xml +++ b/app/src/main/res/layout/activity_settings.xml @@ -49,7 +49,7 @@ app:layout_constraintBottom_toBottomOf="@+id/clock_alignment_label" /> + app:layout_constraintStart_toEndOf="@+id/home_app_alignment_label" + app:layout_constraintTop_toTopOf="@+id/home_app_alignment_label" /> + app:layout_constraintBottom_toBottomOf="@+id/home_app_alignment_label" /> + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/app_item_layout.xml b/app/src/main/res/layout/app_item_layout.xml index 4bb04c8..cf74fe9 100644 --- a/app/src/main/res/layout/app_item_layout.xml +++ b/app/src/main/res/layout/app_item_layout.xml @@ -54,6 +54,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="false" + android:drawablePadding="3dp" android:gravity="start" android:paddingLeft="20dp" android:paddingTop="20dp" @@ -139,4 +140,4 @@ - + \ No newline at end of file