diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e8521d7..f4c83b3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,7 +4,7 @@ + + \ No newline at end of file diff --git a/app/src/main/java/com/finogeeks/mop/demo/InputContentActivity.java b/app/src/main/java/com/finogeeks/mop/demo/InputContentActivity.java new file mode 100644 index 0000000..4933275 --- /dev/null +++ b/app/src/main/java/com/finogeeks/mop/demo/InputContentActivity.java @@ -0,0 +1,40 @@ +package com.finogeeks.mop.demo; + +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Toast; + +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; + +public class InputContentActivity extends AppCompatActivity { + + public static final String EXTRA_NAME_INPUT_CONTENT = "input_content"; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_input_content); + Toolbar toolbar = findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + final EditText editTextInputContent = findViewById(R.id.edt_input_content); + Button btnConfirm = findViewById(R.id.btn_confirm); + btnConfirm.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (editTextInputContent.length() < 1) { + Toast.makeText(InputContentActivity.this, getString(R.string.fin_clip_input_content_hint), Toast.LENGTH_SHORT).show(); + return; + } + Intent intent = new Intent(); + intent.putExtra(EXTRA_NAME_INPUT_CONTENT, editTextInputContent.getText().toString()); + setResult(RESULT_OK, intent); + finish(); + } + }); + } +} diff --git a/app/src/main/java/com/finogeeks/mop/demo/MainActivity.java b/app/src/main/java/com/finogeeks/mop/demo/MainActivity.java index e9c782b..43d878c 100644 --- a/app/src/main/java/com/finogeeks/mop/demo/MainActivity.java +++ b/app/src/main/java/com/finogeeks/mop/demo/MainActivity.java @@ -21,7 +21,7 @@ public class MainActivity extends AppCompatActivity { Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); - Button btnCharts = findViewById(R.id.btnCharts); + Button btnCharts = findViewById(R.id.btn_charts); btnCharts.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -29,7 +29,7 @@ public class MainActivity extends AppCompatActivity { } }); - Button btnDemo = findViewById(R.id.btnDemo); + Button btnDemo = findViewById(R.id.btn_demo); btnDemo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -37,12 +37,20 @@ public class MainActivity extends AppCompatActivity { } }); - Button btnProfile = findViewById(R.id.btnProfile); + Button btnProfile = findViewById(R.id.btn_profile); btnProfile.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FinAppClient.INSTANCE.getAppletApiManager().startApplet(MainActivity.this, "5fa215459a6a7900019b5cc3"); } }); + + Button btnCustomApi = findViewById(R.id.btn_custom_api); + btnCustomApi.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + FinAppClient.INSTANCE.getAppletApiManager().startApplet(MainActivity.this, "5fc8934aefb8c600019e9747"); + } + }); } } \ No newline at end of file diff --git a/app/src/main/java/com/finogeeks/mop/demo/MopApplication.java b/app/src/main/java/com/finogeeks/mop/demo/MopApplication.java index 82a3465..e8dc925 100644 --- a/app/src/main/java/com/finogeeks/mop/demo/MopApplication.java +++ b/app/src/main/java/com/finogeeks/mop/demo/MopApplication.java @@ -8,6 +8,7 @@ import androidx.multidex.MultiDexApplication; import com.finogeeks.lib.applet.client.FinAppClient; import com.finogeeks.lib.applet.client.FinAppConfig; import com.finogeeks.lib.applet.interfaces.FinCallback; +import com.finogeeks.mop.demo.customapi.CustomApi; public class MopApplication extends MultiDexApplication { @@ -38,6 +39,8 @@ public class MopApplication extends MultiDexApplication { @Override public void onSuccess(Object result) { Log.d(TAG, "init result : " + result); + // 注册小程序自定义API + FinAppClient.INSTANCE.getExtensionApiManager().registerApi(new CustomApi(MopApplication.this)); } @Override diff --git a/app/src/main/java/com/finogeeks/mop/demo/customapi/CustomApi.java b/app/src/main/java/com/finogeeks/mop/demo/customapi/CustomApi.java new file mode 100644 index 0000000..b689eae --- /dev/null +++ b/app/src/main/java/com/finogeeks/mop/demo/customapi/CustomApi.java @@ -0,0 +1,85 @@ +package com.finogeeks.mop.demo.customapi; + +import android.content.Context; +import android.content.Intent; + +import androidx.annotation.NonNull; + +import com.finogeeks.lib.applet.api.AbsApi; +import com.finogeeks.lib.applet.interfaces.ICallback; +import com.finogeeks.mop.demo.InputContentActivity; +import com.finogeeks.mop.demo.R; + +import org.json.JSONException; +import org.json.JSONObject; + +import static android.app.Activity.RESULT_OK; +import static com.finogeeks.mop.demo.InputContentActivity.EXTRA_NAME_INPUT_CONTENT; + +/** + * 自定义小程序API + * 跳转到原生APP中输入内容的页面,获取到输入内容后将数据回传给小程序 + */ +public class CustomApi extends AbsApi { + + private static final int REQ_CODE_INPUT = 0x01; + + private static final String API_NAME_ON_NATIVE = "onNative"; + + @NonNull + private Context mContext; + + public CustomApi(@NonNull Context context) { + mContext = context; + } + + /** + * @return 支持可调用的api名称的数组 + */ + @Override + public String[] apis() { + return new String[]{API_NAME_ON_NATIVE}; + } + + /** + * 接收到对应的api调用时,会调用此方法,在此方法中处理api调用的功能逻辑 + * + * @param event 事件名称,即api名称 + * @param param 参数 + * @param callback 回调接口 + */ + @Override + public void invoke(String event, JSONObject param, ICallback callback) { + if (API_NAME_ON_NATIVE.equals(event)) { + Intent intent = new Intent(mContext, InputContentActivity.class); + callback.startActivityForResult(intent, REQ_CODE_INPUT); + } + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data, ICallback callback) { + super.onActivityResult(requestCode, resultCode, data, callback); + if (requestCode == REQ_CODE_INPUT) { + if (resultCode == RESULT_OK && data != null) { + String inputContent = data.getStringExtra(EXTRA_NAME_INPUT_CONTENT); + JSONObject jsonObject = new JSONObject(); + try { + jsonObject.put("text", inputContent); + callback.onSuccess(jsonObject); + } catch (JSONException e) { + e.printStackTrace(); + callback.onFail(); + } + } else { + JSONObject jsonObject = new JSONObject(); + try { + jsonObject.put("errMsg", mContext.getString(R.string.fin_clip_get_input_content_failed)); + callback.onFail(jsonObject); + } catch (JSONException e) { + e.printStackTrace(); + callback.onFail(); + } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_input_content.xml b/app/src/main/res/layout/activity_input_content.xml new file mode 100644 index 0000000..e1e523e --- /dev/null +++ b/app/src/main/res/layout/activity_input_content.xml @@ -0,0 +1,40 @@ + + + + + + + +