1
0
Fork 0

增加自定义API示例

master
xiaoyu 2020-12-03 16:23:33 +08:00
parent 2c6986a7ce
commit 8e3f724f7f
9 changed files with 243 additions and 62 deletions

View File

@ -4,7 +4,7 @@
<application <application
android:name=".MopApplication" android:name=".MopApplication"
android:allowBackup="true" android:allowBackup="false"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
@ -20,5 +20,10 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity
android:name=".InputContentActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" />
</application> </application>
</manifest> </manifest>

View File

@ -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();
}
});
}
}

View File

@ -21,7 +21,7 @@ public class MainActivity extends AppCompatActivity {
Toolbar toolbar = findViewById(R.id.toolbar); Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
Button btnCharts = findViewById(R.id.btnCharts); Button btnCharts = findViewById(R.id.btn_charts);
btnCharts.setOnClickListener(new View.OnClickListener() { btnCharts.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { 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() { btnDemo.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { 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() { btnProfile.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
FinAppClient.INSTANCE.getAppletApiManager().startApplet(MainActivity.this, "5fa215459a6a7900019b5cc3"); 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");
}
});
} }
} }

View File

@ -8,6 +8,7 @@ import androidx.multidex.MultiDexApplication;
import com.finogeeks.lib.applet.client.FinAppClient; import com.finogeeks.lib.applet.client.FinAppClient;
import com.finogeeks.lib.applet.client.FinAppConfig; import com.finogeeks.lib.applet.client.FinAppConfig;
import com.finogeeks.lib.applet.interfaces.FinCallback; import com.finogeeks.lib.applet.interfaces.FinCallback;
import com.finogeeks.mop.demo.customapi.CustomApi;
public class MopApplication extends MultiDexApplication { public class MopApplication extends MultiDexApplication {
@ -38,6 +39,8 @@ public class MopApplication extends MultiDexApplication {
@Override @Override
public void onSuccess(Object result) { public void onSuccess(Object result) {
Log.d(TAG, "init result : " + result); Log.d(TAG, "init result : " + result);
// 注册小程序自定义API
FinAppClient.INSTANCE.getExtensionApiManager().registerApi(new CustomApi(MopApplication.this));
} }
@Override @Override

View File

@ -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};
}
/**
* apiapi
*
* @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();
}
}
}
}
}

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:titleTextColor="@android:color/white" />
<EditText
android:id="@+id/edt_input_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:hint="@string/fin_clip_input_content_hint"
app:layout_constraintBottom_toTopOf="@+id/btn_confirm"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text="@string/fin_clip_confirm"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edt_input_content" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -1,24 +1,63 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
tools:context=".MainActivity"
<com.google.android.material.appbar.AppBarLayout tools:showIn="@layout/activity_main">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar <androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar" android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" /> app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:titleTextColor="@android:color/white" />
</com.google.android.material.appbar.AppBarLayout> <Button
android:id="@+id/btn_charts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fin_clip_charts_applet"
app:layout_constraintBottom_toTopOf="@+id/btn_demo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<include layout="@layout/content_main" /> <Button
android:id="@+id/btn_demo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/fin_clip_demo_applet"
app:layout_constraintBottom_toTopOf="@+id/btn_profile"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_charts" />
</androidx.coordinatorlayout.widget.CoordinatorLayout> <Button
android:id="@+id/btn_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/fin_clip_profile_applet"
app:layout_constraintBottom_toTopOf="@+id/btn_custom_api"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_demo" />
<Button
android:id="@+id/btn_custom_api"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/fin_clip_custom_api"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_profile" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -1,43 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<Button
android:id="@+id/btnCharts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fin_clip_charts_applet"
app:layout_constraintBottom_toTopOf="@+id/btnDemo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<Button
android:id="@+id/btnDemo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/fin_clip_demo_applet"
app:layout_constraintBottom_toTopOf="@+id/btnProfile"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnCharts" />
<Button
android:id="@+id/btnProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/fin_clip_profile_applet"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnDemo" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -1,6 +1,10 @@
<resources> <resources>
<string name="app_name">mopdemo</string> <string name="app_name">FinClip Demo</string>
<string name="fin_clip_charts_applet">绘图小程序</string> <string name="fin_clip_charts_applet">绘图小程序</string>
<string name="fin_clip_demo_applet">官方小程序</string> <string name="fin_clip_demo_applet">官方小程序</string>
<string name="fin_clip_profile_applet">智能对账单</string> <string name="fin_clip_profile_applet">智能对账单</string>
<string name="fin_clip_custom_api">自定义API</string>
<string name="fin_clip_input_content_hint">请输入内容</string>
<string name="fin_clip_confirm">确定</string>
<string name="fin_clip_get_input_content_failed">获取输入内容失败</string>
</resources> </resources>