MainFragment.kt 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package com.sduduzog.slimlauncher.ui.main
  2. import android.animation.ObjectAnimator
  3. import android.arch.lifecycle.Observer
  4. import android.arch.lifecycle.ViewModelProviders
  5. import android.content.*
  6. import android.net.Uri
  7. import android.os.Build
  8. import android.os.Bundle
  9. import android.support.constraint.ConstraintLayout
  10. import android.support.design.widget.BottomSheetBehavior
  11. import android.support.design.widget.BottomSheetBehavior.STATE_COLLAPSED
  12. import android.support.design.widget.BottomSheetBehavior.STATE_HALF_EXPANDED
  13. import android.support.v4.app.Fragment
  14. import android.util.Log
  15. import android.view.LayoutInflater
  16. import android.view.View
  17. import android.view.ViewGroup
  18. import android.widget.Toast
  19. import androidx.navigation.Navigation
  20. import com.daasuu.ei.Ease
  21. import com.daasuu.ei.EasingInterpolator
  22. import com.sduduzog.slimlauncher.MainViewModel
  23. import com.sduduzog.slimlauncher.R
  24. import com.sduduzog.slimlauncher.data.HomeApp
  25. import kotlinx.android.synthetic.main.main_bottom_sheet.*
  26. import kotlinx.android.synthetic.main.main_content.*
  27. import java.text.SimpleDateFormat
  28. import java.util.*
  29. class MainFragment : Fragment() {
  30. private lateinit var viewModel: MainViewModel
  31. private lateinit var receiver: BroadcastReceiver
  32. private lateinit var adapter: MainAppsAdapter
  33. private lateinit var sheetBehavior: BottomSheetBehavior<ConstraintLayout>
  34. private lateinit var themeChooser: ThemeChooserDialog
  35. @Suppress("PropertyName")
  36. val TAG: String = "MainFragment"
  37. override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
  38. savedInstanceState: Bundle?): View {
  39. return inflater.inflate(R.layout.main_fragment, container, false)
  40. }
  41. override fun onActivityCreated(savedInstanceState: Bundle?) {
  42. super.onActivityCreated(savedInstanceState)
  43. sheetBehavior = BottomSheetBehavior.from(bottomSheet)
  44. bottomSheet.alpha = 0.0f
  45. viewModel = ViewModelProviders.of(activity!!).get(MainViewModel::class.java)
  46. adapter = MainAppsAdapter(mutableSetOf(), InteractionHandler())
  47. themeChooser = ThemeChooserDialog.getThemeChooser()
  48. mainAppsList.adapter = adapter
  49. viewModel.homeApps.observe(this, Observer {
  50. if (it != null) {
  51. adapter.setApps(it)
  52. }
  53. })
  54. setEventListeners()
  55. }
  56. override fun onStart() {
  57. super.onStart()
  58. receiver = ClockReceiver()
  59. activity?.registerReceiver(receiver, IntentFilter(Intent.ACTION_TIME_TICK))
  60. doBounceAnimation(ivExpand)
  61. sheetBehavior.state = STATE_COLLAPSED
  62. }
  63. override fun onResume() {
  64. super.onResume()
  65. updateUi()
  66. }
  67. override fun onStop() {
  68. super.onStop()
  69. activity?.unregisterReceiver(receiver)
  70. }
  71. inner class ClockReceiver : BroadcastReceiver() {
  72. override fun onReceive(ctx: Context?, intent: Intent?) {
  73. updateUi()
  74. }
  75. }
  76. fun updateUi() {
  77. val fWatchTime = SimpleDateFormat("HH:mm", Locale.ENGLISH)
  78. val fWatchDate = SimpleDateFormat("EEE, MMM dd", Locale.ENGLISH)
  79. val date = Date()
  80. clockTextView.text = fWatchTime.format(date)
  81. dateTextView.text = fWatchDate.format(date)
  82. doBounceAnimation(ivExpand)
  83. }
  84. inner class InteractionHandler : OnListFragmentInteractionListener {
  85. override fun onLaunch(item: HomeApp) {
  86. val name = ComponentName(item.packageName, item.activityName)
  87. val intent = Intent()
  88. intent.action = Intent.ACTION_MAIN
  89. intent.addCategory(Intent.CATEGORY_LAUNCHER)
  90. intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
  91. intent.component = name
  92. try {
  93. startActivity(intent)
  94. } catch (e: ActivityNotFoundException) {
  95. Log.e(TAG, e.message)
  96. Toast.makeText(activity, "${item.appName} seems to be uninstalled, removing from list", Toast.LENGTH_LONG).show()
  97. viewModel.deleteApp(item)
  98. }
  99. }
  100. }
  101. interface OnListFragmentInteractionListener {
  102. fun onLaunch(item: HomeApp)
  103. }
  104. private fun doBounceAnimation(targetView: View) {
  105. val animator = ObjectAnimator.ofFloat(targetView, "translationY", 0f, -20f, 0f)
  106. animator.interpolator = EasingInterpolator(Ease.QUINT_OUT)
  107. animator.startDelay = 1000
  108. animator.duration = 1000
  109. animator.repeatCount = 1
  110. animator.start()
  111. }
  112. private fun rateApp() {
  113. val uri = Uri.parse("market://details?id=" + context!!.packageName)
  114. val goToMarket = Intent(Intent.ACTION_VIEW, uri)
  115. goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY or
  116. Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
  117. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  118. goToMarket.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
  119. }
  120. try {
  121. startActivity(goToMarket)
  122. Log.d(TAG, goToMarket.data?.query)
  123. } catch (e: ActivityNotFoundException) {
  124. startActivity(Intent(Intent.ACTION_VIEW,
  125. Uri.parse("http://play.google.com/store/apps/details?id=" + context!!.packageName)))
  126. }
  127. }
  128. private fun openSettings() {
  129. startActivity(Intent(android.provider.Settings.ACTION_SETTINGS))
  130. }
  131. private fun changeTheme() {
  132. themeChooser.show(fragmentManager, TAG)
  133. }
  134. private fun setEventListeners(){
  135. clockTextView.setOnClickListener {
  136. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  137. try {
  138. val intent = Intent(android.provider.AlarmClock.ACTION_SHOW_ALARMS)
  139. intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
  140. startActivity(intent)
  141. } catch (e: Exception) {
  142. Log.e(TAG, e.message)
  143. }
  144. }
  145. }
  146. bottomSheet.setOnClickListener {
  147. if (sheetBehavior.state == STATE_COLLAPSED) sheetBehavior.state = STATE_HALF_EXPANDED
  148. }
  149. sheetBehavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
  150. override fun onSlide(p0: View, p1: Float) {
  151. val alpha = 3 * p1
  152. bottomSheet.alpha = alpha
  153. }
  154. override fun onStateChanged(bottomSheet: View, newState: Int) {
  155. ivExpand.visibility = View.INVISIBLE
  156. if (newState == STATE_COLLAPSED) {
  157. ivExpand.visibility = View.VISIBLE
  158. }
  159. }
  160. })
  161. changeThemeText.setOnClickListener { changeTheme() }
  162. settingsText.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_openSettingsFragment))
  163. deviceSettingsText.setOnClickListener { openSettings() }
  164. rateAppText.setOnClickListener { rateApp() }
  165. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  166. changeLauncherText.setOnClickListener {
  167. startActivity(Intent(android.provider.Settings.ACTION_HOME_SETTINGS))
  168. }
  169. } else changeLauncherText.visibility = View.INVISIBLE
  170. aboutText.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_openAboutFragment))
  171. }
  172. }