Sfoglia il codice sorgente

activity animation when starting an activity

sduduzog 7 anni fa
parent
commit
de3f81929d

+ 2 - 5
app/src/main/java/com/sduduzog/slimlauncher/MainActivity.kt

@@ -15,13 +15,9 @@ import com.sduduzog.slimlauncher.ui.main.MainViewModel
 
 class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener, NavController.OnDestinationChangedListener {
 
-    // TODO: Hide and show status bar (possibly bottom nav too) in preferences
-    // TODO: Click on date, opens calendar app
-
-    // TODO: Setup Wizard redesign to include,
-    // TODO: Move some apps to bottom sheet.
     // TODO: Clickable apps while in preferences, intuitiveness
     // TODO: Lock screen on double tap
+    // TODO: Move some apps to bottom sheet.
 
     private lateinit var settings: SharedPreferences
     private val label = "main_fragment"
@@ -67,6 +63,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
 
     override fun onResume() {
         super.onResume()
+        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
         toggleStatusBar()
     }
 

+ 10 - 1
app/src/main/java/com/sduduzog/slimlauncher/ui/main/HomeAppsAdapter.kt

@@ -13,6 +13,10 @@ import androidx.lifecycle.ViewModelProviders
 import androidx.recyclerview.widget.RecyclerView
 import com.sduduzog.slimlauncher.R
 import com.sduduzog.slimlauncher.data.HomeApp
+import androidx.core.content.ContextCompat.startActivity
+import android.app.ActivityOptions
+import androidx.core.app.ActivityOptionsCompat
+
 
 class HomeAppsAdapter(private var fragment: MainFragment)
     : RecyclerView.Adapter<HomeAppsAdapter.ViewHolder>() {
@@ -46,7 +50,12 @@ class HomeAppsAdapter(private var fragment: MainFragment)
             intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
             intent.component = name
             try {
-                fragment.startActivity(intent)
+                val left = 0
+                val top = 0
+                val width = it.measuredWidth
+                val height = it.measuredHeight
+                val opts = ActivityOptionsCompat.makeClipRevealAnimation(it, left, top, width, height)
+                fragment.startActivity(intent, opts.toBundle())
             } catch (e: ActivityNotFoundException) {
                 Toast.makeText(fragment.context, "${item.appName} seems to be uninstalled, removing from list", Toast.LENGTH_LONG).show()
                 viewModel.deleteApp(item)

+ 101 - 22
app/src/main/java/com/sduduzog/slimlauncher/ui/main/MainFragment.kt

@@ -1,5 +1,6 @@
 package com.sduduzog.slimlauncher.ui.main
 
+import android.annotation.SuppressLint
 import android.content.*
 import android.content.pm.PackageManager
 import android.net.Uri
@@ -8,11 +9,11 @@ import android.os.Bundle
 import android.provider.AlarmClock
 import android.provider.MediaStore
 import android.provider.Settings
-import android.view.LayoutInflater
-import android.view.View
-import android.view.ViewGroup
+import android.util.Log
+import android.view.*
 import android.widget.FrameLayout
 import androidx.core.app.ActivityCompat
+import androidx.core.app.ActivityOptionsCompat
 import androidx.core.content.ContextCompat
 import androidx.navigation.Navigation
 import com.google.android.material.bottomsheet.BottomSheetBehavior
@@ -31,6 +32,7 @@ class MainFragment : StatusBarThemeFragment(), MainActivity.OnBackPressedListene
 
     private lateinit var receiver: BroadcastReceiver
     private lateinit var sheetBehavior: BottomSheetBehavior<FrameLayout>
+    private val homeClickListener = HomeDoubleClickListener()
 
     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                               savedInstanceState: Bundle?): View {
@@ -91,20 +93,45 @@ class MainFragment : StatusBarThemeFragment(), MainActivity.OnBackPressedListene
     }
 
     private fun setEventListeners() {
+
+        main.setOnTouchListener(homeClickListener)
+
+        mainAppsList.setOnTouchListener(homeClickListener)
+
         clockTextView.setOnClickListener {
             try {
                 val intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                     Intent(AlarmClock.ACTION_SHOW_ALARMS)
                 } else {
-                    TODO("VERSION.SDK_INT < KITKAT")
+                    alternativeClockIntent()
                 }
                 intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
-                startActivity(intent)
+                val left = 0
+                val top = 0
+                val width = it.measuredWidth
+                val height = it.measuredHeight
+                val opts = ActivityOptionsCompat.makeClipRevealAnimation(it, left, top, width, height)
+                startActivity(intent, opts.toBundle())
             } finally {
                 // Do nothing
             }
         }
 
+        dateTextView.setOnClickListener {
+            try {
+                val intent = Intent(Intent.ACTION_MAIN)
+                intent.addCategory(Intent.CATEGORY_APP_CALENDAR)
+                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
+                val left = 0
+                val top = 0
+                val width = it.measuredWidth
+                val height = it.measuredHeight
+                val opts = ActivityOptionsCompat.makeClipRevealAnimation(it, left, top, width, height)
+                startActivity(intent, opts.toBundle())
+            } finally {
+                // Do nothing
+            }
+        }
 
         val settings = context!!.getSharedPreferences(getString(R.string.prefs_settings), Context.MODE_PRIVATE)
         val isChecked = settings.getBoolean(getString(R.string.prefs_settings_key_app_dialer), false)
@@ -114,7 +141,12 @@ class MainFragment : StatusBarThemeFragment(), MainActivity.OnBackPressedListene
             } else {
                 try {
                     val intent = Intent(Intent.ACTION_DIAL)
-                    startActivity(intent)
+                    val left = 0
+                    val top = 0
+                    val width = it.measuredWidth
+                    val height = it.measuredHeight
+                    val opts = ActivityOptionsCompat.makeClipRevealAnimation(it, left, top, width, height)
+                    startActivity(intent, opts.toBundle())
                 } catch (e: Exception) {
                     // Do nothing
                 }
@@ -124,7 +156,12 @@ class MainFragment : StatusBarThemeFragment(), MainActivity.OnBackPressedListene
             if (isChecked) {
                 try {
                     val intent = Intent(Intent.ACTION_DIAL, null)
-                    startActivity(intent)
+                    val left = 0
+                    val top = 0
+                    val width = it.measuredWidth
+                    val height = it.measuredHeight
+                    val opts = ActivityOptionsCompat.makeClipRevealAnimation(it, left, top, width, height)
+                    startActivity(intent, opts.toBundle())
                 } catch (e: ActivityNotFoundException) {
                     // Do nothing
                 }
@@ -138,7 +175,12 @@ class MainFragment : StatusBarThemeFragment(), MainActivity.OnBackPressedListene
         ivCamera.setOnClickListener {
             try {
                 val intent = Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA)
-                startActivity(intent)
+                val left = 0
+                val top = 0
+                val width = it.measuredWidth
+                val height = it.measuredHeight
+                val opts = ActivityOptionsCompat.makeClipRevealAnimation(it, left, top, width, height)
+                startActivity(intent, opts.toBundle())
             } catch (e: Exception) {
                 // Do nothing
             }
@@ -171,14 +213,14 @@ class MainFragment : StatusBarThemeFragment(), MainActivity.OnBackPressedListene
 
         rateAppText.setOnClickListener {
             val uri = Uri.parse("market://details?id=" + context?.packageName)
-            val goToMarket = Intent(Intent.ACTION_VIEW, uri)
-            goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY or
+            val intent = Intent(Intent.ACTION_VIEW, uri)
+            intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY or
                     Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
-                goToMarket.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
+                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
             }
             try {
-                startActivity(goToMarket)
+                startActivity(intent)
             } catch (e: ActivityNotFoundException) {
                 startActivity(Intent(Intent.ACTION_VIEW,
                         Uri.parse("http://play.google.com/store/apps/details?id=" + context?.packageName)))
@@ -201,17 +243,38 @@ class MainFragment : StatusBarThemeFragment(), MainActivity.OnBackPressedListene
         }
     }
 
-    private fun doBounceAnimation(targetView: View) {
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
-            targetView.animate()
-                    .setStartDelay(500)
-                    .translationYBy(-20f).withEndAction {
-                        targetView.animate()
-                                .setStartDelay(0)
-                                .translationYBy(20f).duration = 100
-                    }.duration = 100
+    private fun alternativeClockIntent(): Intent {
+        val alarmClockIntent = Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER)
+
+// Verify clock implementation
+        val clockImpls = arrayOf(arrayOf("HTC Alarm Clock", "com.htc.android.worldclock", "com.htc.android.worldclock.WorldClockTabControl"), arrayOf("Standar Alarm Clock", "com.android.deskclock", "com.android.deskclock.AlarmClock"), arrayOf("Froyo Nexus Alarm Clock", "com.google.android.deskclock", "com.android.deskclock.DeskClock"), arrayOf("Moto Blur Alarm Clock", "com.motorola.blur.alarmclock", "com.motorola.blur.alarmclock.AlarmClock"), arrayOf("Samsung Galaxy Clock", "com.sec.android.app.clockpackage", "com.sec.android.app.clockpackage.ClockPackage"), arrayOf("Sony Ericsson Xperia Z", "com.sonyericsson.organizer", "com.sonyericsson.organizer.Organizer_WorldClock"), arrayOf("ASUS Tablets", "com.asus.deskclock", "com.asus.deskclock.DeskClock"))
+
+        var foundClockImpl = false
+
+        for (i in clockImpls.indices) {
+            val packageName = clockImpls[i][1]
+            val className = clockImpls[i][2]
+            val cn = ComponentName(packageName, className)
+            alarmClockIntent.component = cn
+            foundClockImpl = true
+        }
+
+        if (!foundClockImpl) {
+            throw Exception()
         }
-        // TODO Animations for API level 16
+        return alarmClockIntent
+
+    }
+
+    private fun doBounceAnimation(targetView: View) {
+        targetView.animate()
+                .setStartDelay(500)
+                .translationYBy(-20f).withEndAction {
+                    targetView.animate()
+                            .setStartDelay(0)
+                            .translationYBy(20f).duration = 100
+                }.duration = 100
+
     }
 
     fun updateUi() {
@@ -247,6 +310,22 @@ class MainFragment : StatusBarThemeFragment(), MainActivity.OnBackPressedListene
         }
     }
 
+    inner class HomeDoubleClickListener : View.OnTouchListener {
+
+        private val gestureDetector = GestureDetector(context, object : GestureDetector.SimpleOnGestureListener() {
+            override fun onDoubleTap(e: MotionEvent?): Boolean {
+                Log.d("MAIN", "double click")
+                return super.onDoubleTap(e)
+            }
+        })
+
+        @SuppressLint("ClickableViewAccessibility")
+        override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
+            gestureDetector.onTouchEvent(p1)
+            return true
+        }
+    }
+
     companion object {
         const val REQUEST_PHONE_CALL = 1
     }

+ 3 - 2
app/src/main/res/layout/main_content.xml

@@ -14,7 +14,7 @@
         android:layout_height="wrap_content"
         android:text="@string/main_placeholder_clock"
         android:layout_marginTop="@dimen/_64sdp"
-        android:textSize="@dimen/_38sdp"
+        android:textSize="@dimen/_40ssp"
         app:layout_constraintBottom_toTopOf="@+id/dateTextView"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintHorizontal_bias="0.5"
@@ -50,6 +50,7 @@
         android:layout_height="wrap_content"
         android:layout_marginStart="16dp"
         android:layout_marginEnd="16dp"
+        android:layout_marginRight="16dp"
         app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
         app:layout_constraintBottom_toTopOf="@+id/guideline2"
         app:layout_constraintEnd_toEndOf="parent"
@@ -64,7 +65,7 @@
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
-        app:layout_constraintGuide_begin="@dimen/_135sdp" />
+        app:layout_constraintGuide_begin="@dimen/_140sdp" />
 
     <androidx.constraintlayout.widget.Guideline
         android:id="@+id/guideline2"

+ 2 - 0
app/src/main/res/values-sw600dp/dimens.xml

@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources></resources>