Skip to content

Commit

Permalink
refatcor: 收拢wmpf初始化&调用的逻辑, 方便开发者接入开发.
Browse files Browse the repository at this point in the history
  • Loading branch information
tummyhuang committed Dec 1, 2020
1 parent 0773d22 commit d9ba896
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 47 deletions.
Binary file removed wmpf-demo/app/libs/wmpf-cli-release-v1.0.4-rc3.aar
Binary file not shown.
Binary file added wmpf-demo/app/libs/wmpf-cli-release.aar
Binary file not shown.
31 changes: 28 additions & 3 deletions wmpf-demo/app/src/main/java/com/tencent/wmpf/demo/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

package com.tencent.wmpf.demo

import android.app.Application
import android.util.Log
import com.tencent.luggage.demo.wxapi.DeviceInfo
import com.tencent.mm.ipcinvoker.IPCInvokeCallbackEx
import com.tencent.wmpf.app.WMPFBoot
import com.tencent.wmpf.cli.task.*
import com.tencent.wmpf.cli.task.pb.WMPFBaseRequestHelper
import com.tencent.wmpf.cli.task.pb.WMPFIPCInvoker
import com.tencent.wmpf.cli.task.pb.proto.WMPFResponse
import com.tencent.wmpf.demo.utils.InvokeTokenHelper
import com.tencent.wmpf.proto.*
import com.tencent.wmpf.utils.WMPFHelper
import io.reactivex.Observable
Expand All @@ -23,6 +24,30 @@ object Api {
return response != null && response.baseResponse.errCode == TaskError.ErrType_OK
}

fun init(context: Application) {
WMPFBoot.init(context)
val invokeToken = getInvokeToken()
WMPFIPCInvoker.initInvokeToken(invokeToken)
}

private fun getInvokeToken(): String {
if (WMPFBoot.getAppContext() == null) {
throw java.lang.Exception("need invoke Api.Init")
}
val pref = WMPFBoot.getAppContext()!!.getSharedPreferences("InvokeTokenHelper", 0)
return pref?.getString(TAG, "")!!
}

private fun initInvokeToken(invokeToken: String) {
if (WMPFBoot.getAppContext() == null) {
throw java.lang.Exception("need invoke Api.Init")
}
val pref = WMPFBoot.getAppContext()!!.getSharedPreferences("InvokeTokenHelper", 0)
val editor = pref?.edit()
editor?.putString(TAG, invokeToken)?.apply()
WMPFIPCInvoker.initInvokeToken(invokeToken)
}

private fun createTaskError(response: WMPFResponse?): TaskError {
if (response == null) {
return TaskError(TaskError.ErrType_NORMAL, -1, "response is null")
Expand Down Expand Up @@ -62,7 +87,7 @@ object Api {
it.onSuccess(response)

if (response != null && !response.invokeToken.isNullOrEmpty()) {
InvokeTokenHelper.initInvokeToken(response.invokeToken)
initInvokeToken(response.invokeToken)
}
} else {
it.onError(TaskErrorException(createTaskError(response)))
Expand Down Expand Up @@ -106,7 +131,7 @@ object Api {
it.onSuccess(response)

if (response != null && !response.invokeToken.isNullOrEmpty()) {
InvokeTokenHelper.initInvokeToken(response.invokeToken)
initInvokeToken(response.invokeToken)
}
} else {
it.onError(TaskErrorException(createTaskError(response)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import com.tencent.mmkv.MMKV;
import com.tencent.wmpf.app.WMPFApplication;
import com.tencent.wmpf.demo.utils.InvokeTokenHelper;

/**
* For 4.4 multi dex support
Expand All @@ -16,12 +15,18 @@ protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);

InvokeTokenHelper.INSTANCE.initInvokeToken(this);

String rootDir = MMKV.initialize(this);
System.out.println("mmkv root: " + rootDir);
}

@Override
public void onCreate() {
super.onCreate();

// 只需要关注Api类中的方法即可跑通WMPF
Api.INSTANCE.init(this);
}

@Override
public void onTerminate() {
super.onTerminate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.tencent.luggage.demo.wxapi.DeviceInfo
import com.tencent.wmpf.cli.task.IPCInvokerTask_InitGlobalConfig
import com.tencent.wmpf.demo.Api
import com.tencent.wmpf.demo.R
import com.tencent.wmpf.demo.utils.InvokeTokenHelper
import io.reactivex.schedulers.Schedulers
import org.json.JSONObject

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ import com.tencent.wmpf.cli.task.pb.WMPFBaseRequestHelper
import com.tencent.wmpf.demo.Api
import com.tencent.wmpf.demo.R
import com.tencent.wmpf.demo.RequestsRepo
import com.tencent.wmpf.demo.utils.InvokeTokenHelper
import com.tencent.wmpf.proto.*
import com.tencent.wxapi.test.OpenSdkTestUtil
import io.reactivex.schedulers.Schedulers
import java.util.*

class FastExperienceActivity : AppCompatActivity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import com.tencent.wmpf.cli.task.*
import com.tencent.wmpf.cli.task.pb.WMPFBaseRequestHelper
import com.tencent.wmpf.demo.Api
import com.tencent.wmpf.demo.R
import com.tencent.wmpf.demo.utils.InvokeTokenHelper
import com.tencent.wmpf.proto.*
import com.tencent.wxapi.test.OpenSdkTestUtil
import io.reactivex.android.schedulers.AndroidSchedulers
Expand Down

This file was deleted.

0 comments on commit d9ba896

Please sign in to comment.