|
@@ -1,14 +1,17 @@
|
|
|
package com.sduduzog.slimlauncher.ui.main
|
|
package com.sduduzog.slimlauncher.ui.main
|
|
|
|
|
|
|
|
import android.content.*
|
|
import android.content.*
|
|
|
|
|
+import android.content.pm.LauncherApps
|
|
|
import android.os.Bundle
|
|
import android.os.Bundle
|
|
|
|
|
+import android.os.UserManager
|
|
|
import android.provider.AlarmClock
|
|
import android.provider.AlarmClock
|
|
|
|
|
+import android.provider.CalendarContract
|
|
|
import android.provider.MediaStore
|
|
import android.provider.MediaStore
|
|
|
import android.view.LayoutInflater
|
|
import android.view.LayoutInflater
|
|
|
import android.view.View
|
|
import android.view.View
|
|
|
import android.view.ViewGroup
|
|
import android.view.ViewGroup
|
|
|
|
|
+import androidx.fragment.app.viewModels
|
|
|
import androidx.lifecycle.Observer
|
|
import androidx.lifecycle.Observer
|
|
|
-import androidx.lifecycle.ViewModelProvider
|
|
|
|
|
import androidx.navigation.Navigation
|
|
import androidx.navigation.Navigation
|
|
|
import com.sduduzog.slimlauncher.R
|
|
import com.sduduzog.slimlauncher.R
|
|
|
import com.sduduzog.slimlauncher.adapters.HomeAdapter
|
|
import com.sduduzog.slimlauncher.adapters.HomeAdapter
|
|
@@ -16,24 +19,18 @@ import com.sduduzog.slimlauncher.models.HomeApp
|
|
|
import com.sduduzog.slimlauncher.models.MainViewModel
|
|
import com.sduduzog.slimlauncher.models.MainViewModel
|
|
|
import com.sduduzog.slimlauncher.utils.BaseFragment
|
|
import com.sduduzog.slimlauncher.utils.BaseFragment
|
|
|
import com.sduduzog.slimlauncher.utils.OnLaunchAppListener
|
|
import com.sduduzog.slimlauncher.utils.OnLaunchAppListener
|
|
|
-import dagger.android.support.AndroidSupportInjection
|
|
|
|
|
|
|
+import dagger.hilt.android.AndroidEntryPoint
|
|
|
import kotlinx.android.synthetic.main.home_fragment.*
|
|
import kotlinx.android.synthetic.main.home_fragment.*
|
|
|
|
|
+import java.text.DateFormat
|
|
|
import java.text.SimpleDateFormat
|
|
import java.text.SimpleDateFormat
|
|
|
import java.util.*
|
|
import java.util.*
|
|
|
-import javax.inject.Inject
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
|
|
+@AndroidEntryPoint
|
|
|
class HomeFragment : BaseFragment(), OnLaunchAppListener {
|
|
class HomeFragment : BaseFragment(), OnLaunchAppListener {
|
|
|
|
|
|
|
|
- @Inject
|
|
|
|
|
- internal lateinit var viewModelFactory: ViewModelProvider.Factory
|
|
|
|
|
private lateinit var receiver: BroadcastReceiver
|
|
private lateinit var receiver: BroadcastReceiver
|
|
|
- private lateinit var viewModel: MainViewModel
|
|
|
|
|
|
|
+ private val viewModel: MainViewModel by viewModels()
|
|
|
|
|
|
|
|
- override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
|
- super.onCreate(savedInstanceState)
|
|
|
|
|
- AndroidSupportInjection.inject(this)
|
|
|
|
|
- }
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
|
|
|
savedInstanceState: Bundle?): View {
|
|
savedInstanceState: Bundle?): View {
|
|
|
return inflater.inflate(R.layout.home_fragment, container, false)
|
|
return inflater.inflate(R.layout.home_fragment, container, false)
|
|
@@ -46,10 +43,6 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {
|
|
|
home_fragment_list.adapter = adapter1
|
|
home_fragment_list.adapter = adapter1
|
|
|
home_fragment_list_exp.adapter = adapter2
|
|
home_fragment_list_exp.adapter = adapter2
|
|
|
|
|
|
|
|
- activity?.let {
|
|
|
|
|
- viewModel = ViewModelProvider(it, viewModelFactory).get(MainViewModel::class.java)
|
|
|
|
|
- } ?: throw Error("Activity null, something here is fucked up")
|
|
|
|
|
-
|
|
|
|
|
viewModel.apps.observe(viewLifecycleOwner, Observer { list ->
|
|
viewModel.apps.observe(viewLifecycleOwner, Observer { list ->
|
|
|
list?.let { apps ->
|
|
list?.let { apps ->
|
|
|
adapter1.setItems(apps.filter {
|
|
adapter1.setItems(apps.filter {
|
|
@@ -86,16 +79,11 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {
|
|
|
|
|
|
|
|
private fun setEventListeners() {
|
|
private fun setEventListeners() {
|
|
|
|
|
|
|
|
- home_fragment_time.setOnClickListener { view ->
|
|
|
|
|
|
|
+ home_fragment_time.setOnClickListener {
|
|
|
try {
|
|
try {
|
|
|
- val pm = context?.packageManager!!
|
|
|
|
|
val intent = Intent(AlarmClock.ACTION_SHOW_ALARMS)
|
|
val intent = Intent(AlarmClock.ACTION_SHOW_ALARMS)
|
|
|
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
|
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
|
|
- val componentName = intent.resolveActivity(pm)
|
|
|
|
|
- if (componentName == null) launchActivity(view, intent) else
|
|
|
|
|
- pm.getLaunchIntentForPackage(componentName.packageName)?.let {
|
|
|
|
|
- launchActivity(view, it)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ launchActivity(it, intent)
|
|
|
} catch (e: ActivityNotFoundException) {
|
|
} catch (e: ActivityNotFoundException) {
|
|
|
e.printStackTrace()
|
|
e.printStackTrace()
|
|
|
// Do nothing, we've failed :(
|
|
// Do nothing, we've failed :(
|
|
@@ -104,8 +92,8 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {
|
|
|
|
|
|
|
|
home_fragment_date.setOnClickListener {
|
|
home_fragment_date.setOnClickListener {
|
|
|
try {
|
|
try {
|
|
|
- val intent = Intent(Intent.ACTION_MAIN)
|
|
|
|
|
- intent.addCategory(Intent.CATEGORY_APP_CALENDAR)
|
|
|
|
|
|
|
+ val builder = CalendarContract.CONTENT_URI.buildUpon().appendPath("time")
|
|
|
|
|
+ val intent = Intent(Intent.ACTION_VIEW, builder.build())
|
|
|
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
|
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
|
|
launchActivity(it, intent)
|
|
launchActivity(it, intent)
|
|
|
} catch (e: ActivityNotFoundException) {
|
|
} catch (e: ActivityNotFoundException) {
|
|
@@ -138,32 +126,31 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
fun updateClock() {
|
|
fun updateClock() {
|
|
|
- val twenty4Hour = context?.getSharedPreferences(getString(R.string.prefs_settings), Context.MODE_PRIVATE)
|
|
|
|
|
- ?.getBoolean(getString(R.string.prefs_settings_key_time_format), true)
|
|
|
|
|
|
|
+ val active = context?.getSharedPreferences(getString(R.string.prefs_settings), Context.MODE_PRIVATE)
|
|
|
|
|
+ ?.getInt(getString(R.string.prefs_settings_key_time_format), 0)
|
|
|
val date = Date()
|
|
val date = Date()
|
|
|
- if (twenty4Hour as Boolean) {
|
|
|
|
|
- val fWatchTime = SimpleDateFormat("h:mm aa", Locale.ROOT)
|
|
|
|
|
- home_fragment_time.text = fWatchTime.format(date)
|
|
|
|
|
- } else {
|
|
|
|
|
- val fWatchTime = SimpleDateFormat("H:mm", Locale.ROOT)
|
|
|
|
|
- home_fragment_time.text = fWatchTime.format(date)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ val fWatchTime = when(active) {
|
|
|
|
|
+ 1 -> SimpleDateFormat("H:mm", Locale.ROOT)
|
|
|
|
|
+ 2 -> SimpleDateFormat("h:mm aa", Locale.ROOT)
|
|
|
|
|
+ else -> DateFormat.getTimeInstance(DateFormat.SHORT)
|
|
|
}
|
|
}
|
|
|
|
|
+ home_fragment_time.text = fWatchTime.format(date)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
val fWatchDate = SimpleDateFormat("EEE, MMM dd", Locale.ROOT)
|
|
val fWatchDate = SimpleDateFormat("EEE, MMM dd", Locale.ROOT)
|
|
|
home_fragment_date.text = fWatchDate.format(date)
|
|
home_fragment_date.text = fWatchDate.format(date)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
override fun onLaunch(app: HomeApp, view: View) {
|
|
override fun onLaunch(app: HomeApp, view: View) {
|
|
|
try {
|
|
try {
|
|
|
- val intent = Intent()
|
|
|
|
|
- val name = ComponentName(app.packageName, app.activityName)
|
|
|
|
|
- intent.action = Intent.ACTION_MAIN
|
|
|
|
|
- intent.addCategory(Intent.CATEGORY_LAUNCHER)
|
|
|
|
|
- intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
|
|
|
|
|
- intent.component = name
|
|
|
|
|
-
|
|
|
|
|
- intent.resolveActivity(activity!!.packageManager)?.let {
|
|
|
|
|
- launchActivity(view, intent)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ val manager = requireContext().getSystemService(Context.USER_SERVICE) as UserManager
|
|
|
|
|
+ val launcher = requireContext().getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
|
|
|
|
|
+
|
|
|
|
|
+ val componentName = ComponentName(app.packageName, app.activityName)
|
|
|
|
|
+ val userHandle = manager.getUserForSerialNumber(app.userSerial)
|
|
|
|
|
+
|
|
|
|
|
+ launcher.startMainActivity(componentName, userHandle, view.clipBounds, null)
|
|
|
} catch (e: Exception) {
|
|
} catch (e: Exception) {
|
|
|
// Do no shit yet
|
|
// Do no shit yet
|
|
|
}
|
|
}
|