feat:Demo List of Android
This commit is contained in:
parent
b9baf938f4
commit
66da6eb13a
@ -19,6 +19,13 @@
|
|||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
<activity android:name=".DemoActivity">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.VIEW" />
|
||||||
|
|
||||||
|
<category android:name="android.intent.category.DEFAULT" />
|
||||||
|
</intent-filter>
|
||||||
|
</activity>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
123
Android/app/src/main/java/pub/doric/demo/DemoActivity.java
Normal file
123
Android/app/src/main/java/pub/doric/demo/DemoActivity.java
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
/*
|
||||||
|
* Copyright [2019] [Doric.Pub]
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package pub.doric.demo;
|
||||||
|
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.view.KeyEvent;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.FrameLayout;
|
||||||
|
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
|
|
||||||
|
import org.greenrobot.eventbus.EventBus;
|
||||||
|
import org.greenrobot.eventbus.Subscribe;
|
||||||
|
import org.greenrobot.eventbus.ThreadMode;
|
||||||
|
|
||||||
|
import pub.doric.DoricContext;
|
||||||
|
import pub.doric.DoricDriver;
|
||||||
|
import pub.doric.dev.DevPanel;
|
||||||
|
import pub.doric.dev.event.EnterDebugEvent;
|
||||||
|
import pub.doric.dev.event.QuitDebugEvent;
|
||||||
|
import pub.doric.engine.ChangeEngineCallback;
|
||||||
|
import pub.doric.utils.DoricUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: pub.doric.demo
|
||||||
|
* @Author: pengfei.zhou
|
||||||
|
* @CreateDate: 2019-11-19
|
||||||
|
*/
|
||||||
|
public class DemoActivity extends AppCompatActivity {
|
||||||
|
private DoricContext doricContext;
|
||||||
|
private DevPanel devPanel = new DevPanel();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
String source = getIntent().getStringExtra("source");
|
||||||
|
FrameLayout frameLayout = new FrameLayout(this);
|
||||||
|
addContentView(frameLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
|
||||||
|
ViewGroup.LayoutParams.MATCH_PARENT));
|
||||||
|
doricContext = DoricContext.create(this, DoricUtils.readAssetFile("demo/" + source), "test");
|
||||||
|
doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
|
||||||
|
doricContext.getRootNode().setRootView(frameLayout);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
doricContext.onShow();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
doricContext.onHidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAttachedToWindow() {
|
||||||
|
super.onAttachedToWindow();
|
||||||
|
|
||||||
|
EventBus.getDefault().register(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onDestroy() {
|
||||||
|
super.onDestroy();
|
||||||
|
doricContext.teardown();
|
||||||
|
EventBus.getDefault().unregister(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||||
|
public void onEnterDebugEvent(EnterDebugEvent enterDebugEvent) {
|
||||||
|
DoricDriver.getInstance().changeJSEngine(false, new ChangeEngineCallback() {
|
||||||
|
@Override
|
||||||
|
public void changed() {
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||||
|
public void onQuitDebugEvent(QuitDebugEvent quitDebugEvent) {
|
||||||
|
DoricDriver.getInstance().changeJSEngine(true, new ChangeEngineCallback() {
|
||||||
|
@Override
|
||||||
|
public void changed() {
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||||
|
if (KeyEvent.KEYCODE_MENU == event.getKeyCode()) {
|
||||||
|
devPanel.show(getSupportFragmentManager(), "DevPanel");
|
||||||
|
}
|
||||||
|
return super.onKeyDown(keyCode, event);
|
||||||
|
}
|
||||||
|
}
|
@ -15,102 +15,92 @@
|
|||||||
*/
|
*/
|
||||||
package pub.doric.demo;
|
package pub.doric.demo;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.KeyEvent;
|
import android.util.TypedValue;
|
||||||
|
import android.view.Gravity;
|
||||||
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.FrameLayout;
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||||
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
import org.greenrobot.eventbus.EventBus;
|
|
||||||
import org.greenrobot.eventbus.Subscribe;
|
|
||||||
import org.greenrobot.eventbus.ThreadMode;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
import pub.doric.dev.DevPanel;
|
|
||||||
import pub.doric.DoricContext;
|
|
||||||
import pub.doric.DoricDriver;
|
|
||||||
import pub.doric.dev.LocalServer;
|
|
||||||
import pub.doric.dev.event.EnterDebugEvent;
|
|
||||||
import pub.doric.dev.event.QuitDebugEvent;
|
|
||||||
import pub.doric.engine.ChangeEngineCallback;
|
|
||||||
import pub.doric.utils.DoricUtils;
|
import pub.doric.utils.DoricUtils;
|
||||||
|
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private DevPanel devPanel = new DevPanel();
|
|
||||||
|
|
||||||
private DoricContext doricContext;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
doricContext = DoricContext.create(this, DoricUtils.readAssetFile("demo/ScrollerDemo.js"), "test");
|
RecyclerView recyclerView = findViewById(R.id.root);
|
||||||
doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
|
recyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||||
doricContext.getRootNode().setRootView((FrameLayout) findViewById(R.id.root));
|
|
||||||
|
|
||||||
LocalServer localServer = new LocalServer(getApplicationContext(), 8910);
|
|
||||||
try {
|
try {
|
||||||
localServer.start();
|
String[] demos = getAssets().list("demo");
|
||||||
|
List<String> ret = new ArrayList<>();
|
||||||
|
for (String str : demos) {
|
||||||
|
if (str.endsWith("js")) {
|
||||||
|
ret.add(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
recyclerView.setAdapter(new MyAdapter(ret.toArray(new String[0])));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public static class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
||||||
public void onAttachedToWindow() {
|
|
||||||
super.onAttachedToWindow();
|
|
||||||
|
|
||||||
EventBus.getDefault().register(this);
|
private final String[] data;
|
||||||
|
|
||||||
|
public MyAdapter(String[] demos) {
|
||||||
|
this.data = demos;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||||
|
TextView textView = new TextView(parent.getContext());
|
||||||
|
textView.setGravity(Gravity.CENTER);
|
||||||
|
textView.setLayoutParams(new ViewGroup.LayoutParams(
|
||||||
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||||
|
DoricUtils.dp2px(50)));
|
||||||
|
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
|
||||||
|
return new RecyclerView.ViewHolder(textView) {
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return super.toString();
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
|
||||||
super.onDestroy();
|
final TextView tv = (TextView) holder.itemView;
|
||||||
|
tv.setText(data[position]);
|
||||||
EventBus.getDefault().unregister(this);
|
tv.setOnClickListener(new View.OnClickListener() {
|
||||||
}
|
|
||||||
|
|
||||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
||||||
public void onEnterDebugEvent(EnterDebugEvent enterDebugEvent) {
|
|
||||||
DoricDriver.getInstance().changeJSEngine(false, new ChangeEngineCallback() {
|
|
||||||
@Override
|
@Override
|
||||||
public void changed() {
|
public void onClick(View v) {
|
||||||
runOnUiThread(new Runnable() {
|
Intent intent = new Intent(tv.getContext(), DemoActivity.class);
|
||||||
@Override
|
intent.putExtra("source", data[position]);
|
||||||
public void run() {
|
tv.getContext().startActivity(intent);
|
||||||
doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
||||||
public void onQuitDebugEvent(QuitDebugEvent quitDebugEvent) {
|
|
||||||
DoricDriver.getInstance().changeJSEngine(true, new ChangeEngineCallback() {
|
|
||||||
@Override
|
|
||||||
public void changed() {
|
|
||||||
runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
doricContext.init(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
public int getItemCount() {
|
||||||
if (KeyEvent.KEYCODE_MENU == event.getKeyCode()) {
|
return data.length;
|
||||||
devPanel.show(getSupportFragmentManager(), "DevPanel");
|
}
|
||||||
}
|
|
||||||
return super.onKeyDown(keyCode, event);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,8 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
tools:context=".MainActivity">
|
|
||||||
|
|
||||||
<FrameLayout
|
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:id="@+id/root"
|
android:id="@+id/root"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent" />
|
android:layout_height="match_parent"
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
tools:context=".MainActivity" />
|
Reference in New Issue
Block a user