diff --git a/app/src/main/java/chaoran/business/activity/MainActivity.java b/app/src/main/java/chaoran/business/activity/MainActivity.java
index 486a6b4..2ed0f6d 100644
--- a/app/src/main/java/chaoran/business/activity/MainActivity.java
+++ b/app/src/main/java/chaoran/business/activity/MainActivity.java
@@ -1,12 +1,16 @@
package chaoran.business.activity;
import android.annotation.SuppressLint;
+import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
+import android.hardware.Sensor;
+import android.hardware.SensorEvent;
+import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Build;
@@ -16,7 +20,9 @@ import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.OrientationEventListener;
+import android.view.Surface;
import android.view.View;
+import android.view.WindowManager;
import android.webkit.*;
import android.widget.ProgressBar;
import android.widget.Toast;
@@ -25,6 +31,8 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
+import com.google.gson.Gson;
+
import java.io.IOException;
import java.util.Map;
@@ -49,6 +57,12 @@ import chaoran.business.utils.StatusBarUtil;
*/
public class MainActivity extends AppCompatActivity implements ResultListener{
+ public static int SCREEN_ROTATION = 3; // 屏幕旋转的设置
+
+ MyOrientationDetector myOrientationDetector;
+
+ private static int latestOrientation = 0; // 添加一个标志位;最后屏幕的方向
+
private WebView webView;
private Adapter adapter;
private VoiceEngine voiceEngine;
@@ -66,6 +80,8 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
//SCREEN_ORIENTATION_FULL_SENSOR 都可以旋转,但是在不打开旋转的情况下,一样可以旋转
//SCREEN_ORIENTATION_FULL_USER 关闭旋转则不会旋转了
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_USER);
+ myOrientationDetector = new MyOrientationDetector(this, this);
+ myOrientationDetector.enable();
}
@@ -221,6 +237,7 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
// voiceEngine.reload();
super.onResume();
if (adapter != null) {
+ myOrientationDetector.disable();
adapter.stop2();
adapter.start();
}
@@ -287,6 +304,7 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
String address = spf.getString("address", "").replaceAll(" ", "");
String path = spf.getString("path", "").replaceAll(" ", "");
String link = address.concat(":").concat(String.valueOf(port)).concat(path);
+ SCREEN_ROTATION = spf.getInt("screen_rotation", 3);
return link.startsWith("http://") ? link : "http://".concat(link);
}
@@ -357,13 +375,16 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
});
}
- /* 监听角度的变化
+ /* 监听角度的变化 */
class MyOrientationDetector extends OrientationEventListener {
private int rotate;
- public MyOrientationDetector(Context context) {
+ private Activity activity;
+
+ public MyOrientationDetector(Context context, Activity activity) {
super(context);
+ this.activity = activity;
}
@Override
@@ -372,21 +393,104 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
//0-57度 125-236度 306-360度 这些区间范围内为竖屏
//58-124度 237-305度 这些区间范围内为横屏
if (orientation >=0 && orientation <= 57 ) {
- rotate = 1; // 正竖屏
+ rotate = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; // 正竖屏
}else if (orientation >=125 && orientation <= 236 ) {
- rotate = 2; // 倒竖屏
+ rotate = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; // 倒竖屏
}else if (orientation >=306 && orientation <= 360 ) {
- rotate = 1; // 正竖屏
+ rotate = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; // 正竖屏
}else if (orientation >=58 && orientation <= 124 ) {
- rotate = 3; // 正横屏
+ rotate = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; // 正横屏
}else if (orientation >=237 && orientation <= 305 ) {
- rotate = 4; // 倒横屏
+ rotate = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; // 倒横屏
}else {
- rotate = 1; // 默认正竖屏
+ rotate = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; // 默认正竖屏
+ }
+ if (SCREEN_ROTATION == 1 || SCREEN_ROTATION == 3) {
+ if (rotate == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT || rotate == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
+ activity.setRequestedOrientation(rotate);
+ latestOrientation = rotate;
+ }
+ }
+ if (SCREEN_ROTATION == 2 || SCREEN_ROTATION == 3) {
+ if (rotate == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE || rotate == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
+ activity.setRequestedOrientation(rotate);
+ latestOrientation = rotate;
+ }
}
- System.out.println("屏幕旋转角度监控::" + rotate);
}
+
+
+ }
+
+ /*class MyOrientationDetector implements SensorEventListener {
+
+ private Activity activity;
+
+ public MyOrientationDetector(Activity activity) {
+ this.activity = activity;
+ Sensor orientationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
+ sensorManager.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL);
+ }
+
+ @Override
+ public void onSensorChanged(SensorEvent event) {
+
+ int angle = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
+ if (latestOrientation == angle) {
+ return;
+ }
+ if (SCREEN_ROTATION == 1) {
+ if (Surface.ROTATION_0 == angle) {
+ activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
+ latestOrientation = angle;
+ }else if (Surface.ROTATION_180 == angle) {
+ activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
+ latestOrientation = angle;
+ }
+ return;
+ }else if (SCREEN_ROTATION == 2) {
+ if (Surface.ROTATION_90 == angle) {
+ activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
+ latestOrientation = angle;
+ }else if (Surface.ROTATION_270 == angle) {
+ activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
+ latestOrientation = angle;
+ }
+ return;
+ }
+ int tmpAngle = latestOrientation;
+ latestOrientation = angle;
+ switch (angle) {
+ case Surface.ROTATION_0:
+ activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
+ break;
+ case Surface.ROTATION_90:
+ activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
+ break;
+ case Surface.ROTATION_180:
+ activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
+ break;
+ case Surface.ROTATION_270:
+ activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
+ break;
+ default:
+ latestOrientation = tmpAngle;
+ break;
+ }
+ }
+
+ @Override
+ public void onAccuracyChanged(Sensor sensor, int accuracy) {
+
+ }
}*/
+ @SuppressLint("WrongConstant")
+ @Override
+ public void onConfigurationChanged(Configuration newConfig) {
+ super.onConfigurationChanged(newConfig);
+ }
+
+
}
\ No newline at end of file
diff --git a/app/src/main/java/chaoran/business/activity/NetworkSettingActivity.java b/app/src/main/java/chaoran/business/activity/NetworkSettingActivity.java
index 9333e55..17b1820 100644
--- a/app/src/main/java/chaoran/business/activity/NetworkSettingActivity.java
+++ b/app/src/main/java/chaoran/business/activity/NetworkSettingActivity.java
@@ -19,7 +19,7 @@ import chaoran.business.R;
public class NetworkSettingActivity extends AppCompatActivity {
- private EditText address, path, port;
+ private EditText address, path, port, screen_rotation;
private Button save, cancel;
@Override
@@ -35,12 +35,14 @@ public class NetworkSettingActivity extends AppCompatActivity {
address = findViewById(R.id.address);
path = findViewById(R.id.path);
port = findViewById(R.id.port);
+ screen_rotation = findViewById(R.id.screen_rotation);
save = findViewById(R.id.save);
cancel = findViewById(R.id.cancel);
SharedPreferences sharedPreferences = this.getSharedPreferences("crtech", Context.MODE_PRIVATE);
address.setText(sharedPreferences.getString("address", ""));
path.setText(sharedPreferences.getString("path", ""));
port.setText(String.valueOf(sharedPreferences.getInt("port", -1)));
+ screen_rotation.setText(String.valueOf(sharedPreferences.getInt("screen_rotation", 3)));
cancel.setOnClickListener((e) -> this.finish());
save.setOnClickListener((e) -> saveSetting());
}
@@ -50,6 +52,7 @@ public class NetworkSettingActivity extends AppCompatActivity {
editor.putString("address", address.getText().toString().trim());
editor.putString("path", path.getText().toString().trim());
editor.putInt("port", Integer.parseInt(port.getText().toString().trim()));
+ editor.putInt("screen_rotation", Integer.parseInt(screen_rotation.getText().toString().trim()));
editor.commit();
this.finish();
}
diff --git a/app/src/main/java/chaoran/business/engine/entity/NetworkSetting.java b/app/src/main/java/chaoran/business/engine/entity/NetworkSetting.java
index 654e15d..d3700ff 100644
--- a/app/src/main/java/chaoran/business/engine/entity/NetworkSetting.java
+++ b/app/src/main/java/chaoran/business/engine/entity/NetworkSetting.java
@@ -12,6 +12,8 @@ public class NetworkSetting extends Setting {
private Integer port;
private String path;
+ private Integer screen_rotation;
+
public String getAddress() {
return address;
}
@@ -35,4 +37,12 @@ public class NetworkSetting extends Setting {
public void setPath(String path) {
this.path = path;
}
+
+ public Integer getScreen_rotation() {
+ return screen_rotation;
+ }
+
+ public void setScreen_rotation(Integer screen_rotation) {
+ this.screen_rotation = screen_rotation;
+ }
}
\ No newline at end of file
diff --git a/app/src/main/java/chaoran/business/engine/impl/NetworkSettingEngine.java b/app/src/main/java/chaoran/business/engine/impl/NetworkSettingEngine.java
index ca3f308..4c3f749 100644
--- a/app/src/main/java/chaoran/business/engine/impl/NetworkSettingEngine.java
+++ b/app/src/main/java/chaoran/business/engine/impl/NetworkSettingEngine.java
@@ -11,6 +11,8 @@ package chaoran.business.engine.impl;
import android.content.Context;
import android.content.SharedPreferences;
import android.webkit.JavascriptInterface;
+
+import chaoran.business.activity.MainActivity;
import chaoran.business.engine.SettingEngine;
import chaoran.business.engine.entity.NetworkSetting;
import chaoran.business.engine.entity.Setting;
@@ -35,12 +37,14 @@ public class NetworkSettingEngine implements SettingEngine {
public Setting getSetting() {
SharedPreferences spf = context.getSharedPreferences("crtech", Context.MODE_PRIVATE);
Integer port = spf.getInt("port", -1);
+ Integer screen_rotation = spf.getInt("screen_rotation", 3);
String address = spf.getString("address", "").replaceAll(" ", "");
String path = spf.getString("path", "").replaceAll(" ", "");
NetworkSetting networkSetting = new NetworkSetting();
networkSetting.setAddress(address);
networkSetting.setPort(port);
networkSetting.setPath(path);
+ networkSetting.setScreen_rotation(screen_rotation);
return networkSetting;
}
@@ -51,7 +55,9 @@ public class NetworkSettingEngine implements SettingEngine {
editor.putString("address", networkSetting.getAddress());
editor.putString("path", networkSetting.getPath());
editor.putInt("port", networkSetting.getPort());
+ editor.putInt("screen_rotation", networkSetting.getScreen_rotation());
editor.commit();
+ MainActivity.SCREEN_ROTATION = networkSetting.getScreen_rotation();
return true;
}
diff --git a/app/src/main/res/layout/activity_setting_network.xml b/app/src/main/res/layout/activity_setting_network.xml
index de38678..59767b3 100644
--- a/app/src/main/res/layout/activity_setting_network.xml
+++ b/app/src/main/res/layout/activity_setting_network.xml
@@ -7,6 +7,28 @@
android:id="@+id/table">
+
+
+
+
+
+
+
+
+
+
+