Skip to content

Commit

Permalink
feat: add multiple process sample
Browse files Browse the repository at this point in the history
  • Loading branch information
idisfkj committed Sep 15, 2020
1 parent e422cd7 commit b168716
Show file tree
Hide file tree
Showing 12 changed files with 269 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class StartupManager private constructor(
mAwaitCountDownLatch = CountDownLatch(needAwaitCount.get())

if (startupList.isNullOrEmpty()) {
StartupLogUtils.e("startupList is empty in the current process.")
return@apply
}

Expand Down
8 changes: 5 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
</intent-filter>
</activity>

<activity
android:name=".SampleMoreActivity"
android:process=":multiple.provider" />
<activity android:name=".SampleMoreActivity" />

<activity android:name=".SampleCommonActivity" />

<service
android:name=".MultipleProcessService"
android:process=":multiple.process.service" />

<provider
android:name="com.rousetime.android_startup.provider.StartupProvider"
android:authorities="${applicationId}.android_startup"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// IMultipleProcessServiceInterface.aidl
package com.rousetime.sample;

// Declare any non-default types here with import statements
import com.rousetime.sample.IServiceListenerInterface;

interface IMultipleProcessServiceInterface {

void addServiceListener(IServiceListenerInterface serviceListener);

void initStartup();

void clear();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// IServiceListenerInterface.aidl
package com.rousetime.sample;

// Declare any non-default types here with import statements

interface IServiceListenerInterface {

void onCompleted(String result, long totalMainThreadCostTime);

}
97 changes: 97 additions & 0 deletions app/src/main/java/com/rousetime/sample/MultipleProcessService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.rousetime.sample

import android.app.Service
import android.content.Intent
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.util.Log
import com.rousetime.android_startup.AndroidStartup
import com.rousetime.android_startup.StartupListener
import com.rousetime.android_startup.StartupManager
import com.rousetime.android_startup.manager.StartupCacheManager
import com.rousetime.android_startup.model.CostTimesModel
import com.rousetime.android_startup.model.LoggerLevel
import com.rousetime.android_startup.model.StartupConfig
import com.rousetime.sample.startup.multiple.SampleMultipleFifthStartup
import com.rousetime.sample.startup.multiple.SampleMultipleFourthStartup
import com.rousetime.sample.startup.multiple.SampleMultipleSixthStartup
import com.rousetime.sample.startup.multiple.SampleMultipleThirdStartup

/**
* Created by idisfkj on 2020/9/15.
* Email: [email protected].
*/
class MultipleProcessService : Service() {

private var mServiceListener: IServiceListenerInterface? = null

private var mBinder = object : IMultipleProcessServiceInterface.Stub() {

override fun clear() {
StartupCacheManager.instance.clear()
}

override fun initStartup() {
// must be start main thread.
Handler(Looper.getMainLooper()).post {
val list = mutableListOf<AndroidStartup<*>>()
list.add(SampleMultipleThirdStartup())
list.add(SampleMultipleFourthStartup())
list.add(SampleMultipleFifthStartup())
list.add(SampleMultipleSixthStartup())
val config = StartupConfig.Builder()
.setLoggerLevel(LoggerLevel.DEBUG)
.setAwaitTimeout(12000L)
.setListener(object : StartupListener {
override fun onCompleted(totalMainThreadCostTime: Long, costTimesModels: List<CostTimesModel>) {
// can to do cost time statistics.
mServiceListener?.onCompleted(buildString {
append("Startup Completed: ")
append("\n")
append("\n")
costTimesModels.forEach {
append("\n")
append("Startup Name: ${it.name}")
append("\n")
append("CallOnMainThread: ${it.callOnMainThread}")
append("\n")
append("WaitOnMainThread: ${it.waitOnMainThread}")
append("\n")
append("Cost times: ${it.endTime - it.startTime} ms")
append("\n")
}
if (costTimesModels.isEmpty()) {
append("result form cache.")
append("\n")
list.forEach {
append("\n")
append("${it::class.java.simpleName}: ${StartupCacheManager.instance.obtainInitializedResult<String>(it::class.java)}")
append("\n")
}
}
}, totalMainThreadCostTime)
Log.d("StartupTrack", "onCompleted: ${costTimesModels.size}")
}
})
.build()

StartupManager.Builder()
.setConfig(config)
.addAllStartup(list)
.build(this@MultipleProcessService)
.start()
.await()
}
}

override fun addServiceListener(serviceListener: IServiceListenerInterface?) {
mServiceListener = serviceListener
}

}

override fun onBind(intent: Intent?): IBinder? {
return mBinder
}
}
51 changes: 49 additions & 2 deletions app/src/main/java/com/rousetime/sample/SampleCommonActivity.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.rousetime.sample

import android.app.Service
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -21,6 +27,27 @@ import kotlinx.android.synthetic.main.activity_common.*
*/
class SampleCommonActivity : AppCompatActivity() {

private var mMultipleProcessService: IMultipleProcessServiceInterface? = null

private val mMultipleProcessServiceConnection by lazy {
object : ServiceConnection {
override fun onServiceDisconnected(name: ComponentName?) {}

override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
mMultipleProcessService = IMultipleProcessServiceInterface.Stub.asInterface(service)
mMultipleProcessService?.addServiceListener(object : IServiceListenerInterface.Stub() {
override fun onCompleted(result: String?, totalMainThreadCostTime: Long) {
result?.let {
Handler(Looper.getMainLooper()).post {
showResult(result)
}
}
}
})
mMultipleProcessService?.initStartup()
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -31,9 +58,11 @@ class SampleCommonActivity : AppCompatActivity() {

fun onClick(view: View) {
if (R.id.clear == view.id) {
mMultipleProcessService?.clear()
StartupCacheManager.instance.clear()
content.text = getString(R.string.clear_cache_success)
} else if (R.id.get == view.id) {
unbindService()
start()
}
}
Expand Down Expand Up @@ -69,14 +98,17 @@ class SampleCommonActivity : AppCompatActivity() {
list.add(SampleAsyncSevenStartup())
list.add(SampleSyncFiveStartup())
}
R.id.multiply_process -> {
bindService(Intent(this, MultipleProcessService::class.java), mMultipleProcessServiceConnection, Service.BIND_AUTO_CREATE)
}
}
val config = StartupConfig.Builder()
.setLoggerLevel(LoggerLevel.DEBUG)
.setAwaitTimeout(12000L)
.setListener(object : StartupListener {
override fun onCompleted(totalMainThreadCostTime: Long, costTimesModels: List<CostTimesModel>) {
// can to do cost time statistics.
content.text = buildString {
showResult(buildString {
append("Startup Completed: ")
append("\n")
append("\n")
Expand All @@ -100,7 +132,7 @@ class SampleCommonActivity : AppCompatActivity() {
append("\n")
}
}
}
})
Log.d("StartupTrack", "onCompleted: ${costTimesModels.size}")
}
})
Expand All @@ -117,4 +149,19 @@ class SampleCommonActivity : AppCompatActivity() {
.await()
}, 16)
}

private fun showResult(result: String) {
content.text = result
}

private fun unbindService() {
mMultipleProcessService?.let {
unbindService(mMultipleProcessServiceConnection)
}
}

override fun onDestroy() {
unbindService()
super.onDestroy()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.rousetime.sample.startup.multiple

import android.content.Context
import com.rousetime.android_startup.AndroidStartup
import com.rousetime.android_startup.Startup
import com.rousetime.android_startup.annotation.MultipleProcess

/**
* Created by idisfkj on 2020/9/15.
* Email: [email protected].
*/
@MultipleProcess(":multiple.process.service")
class SampleMultipleFifthStartup : AndroidStartup<String>() {

override fun create(context: Context): String? {
return SampleMultipleFifthStartup::class.java.simpleName
}

override fun callCreateOnMainThread(): Boolean = false

override fun waitOnMainThread(): Boolean = false

override fun dependencies(): List<Class<out Startup<*>>>? {
return listOf(SampleMultipleFourthStartup::class.java)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.rousetime.sample.startup.multiple

import android.content.Context
import com.rousetime.android_startup.AndroidStartup
import com.rousetime.android_startup.annotation.MultipleProcess

/**
* Created by idisfkj on 2020/9/15.
* Email: [email protected].
*/
@MultipleProcess(":multiple.process.service")
class SampleMultipleFourthStartup : AndroidStartup<String>() {

override fun create(context: Context): String? {
Thread.sleep(1000)
return SampleMultipleFourthStartup::class.java.simpleName
}

override fun callCreateOnMainThread(): Boolean = false

override fun waitOnMainThread(): Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import com.rousetime.android_startup.annotation.MultipleProcess
* Email: [email protected].
*/
@MultipleProcess(":multiple.provider")
class SampleMultipleSecondStartup : AndroidStartup<Boolean>() {
class SampleMultipleSecondStartup : AndroidStartup<String>() {

override fun create(context: Context): Boolean? {
return true
override fun create(context: Context): String? {
return SampleMultipleSecondStartup::class.java.simpleName
}

override fun callCreateOnMainThread(): Boolean = false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.rousetime.sample.startup.multiple

import android.content.Context
import com.rousetime.android_startup.AndroidStartup
import com.rousetime.android_startup.Startup
import com.rousetime.android_startup.annotation.MultipleProcess

/**
* Created by idisfkj on 2020/9/15.
* Email: [email protected].
*/
@MultipleProcess(":multiple.process.service")
class SampleMultipleSixthStartup : AndroidStartup<String>() {

override fun create(context: Context): String? {
return SampleMultipleSixthStartup::class.java.simpleName
}

override fun callCreateOnMainThread(): Boolean = false

override fun waitOnMainThread(): Boolean = true

override fun dependencies(): List<Class<out Startup<*>>>? {
return listOf(SampleMultipleFifthStartup::class.java)
}

}
13 changes: 13 additions & 0 deletions app/src/main/res/layout/activity_more.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,17 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/async_and_async_await_main_thread" />

<Button
android:id="@+id/multiply_process"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:onClick="onClick"
android:text="@string/multiple_process"
android:textAllCaps="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/manual_dispatch" />

</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
<string name="async_and_async">Async And Async</string>
<string name="async_and_async_await_main_thread">Async And Async Await Main Thread</string>
<string name="manual_dispatch">Manual Dispatch</string>
<string name="multiple_process">Multiple Process</string>
</resources>

0 comments on commit b168716

Please sign in to comment.