Commit 87fb84bd200461a79598404a79058ba466c6d6fb

Authored by 陈硕
2 parents ded909ab 9ccd584b

Merge branch 'master' of bj.gitlab.cnlive.com:cnlive-team/CNLiveApp

app/src/main/assets/cnlive_logo.png 0 → 100644

6.02 KB

app/src/main/java/com/cnlive/gundam/main/ui/activity/MainActivity.java
@@ -20,6 +20,7 @@ import com.cnlive.gundam.main.ui.widget.FragmentTabHost; @@ -20,6 +20,7 @@ import com.cnlive.gundam.main.ui.widget.FragmentTabHost;
20 import com.cnlive.gundam.main.utils.SharedPreferencesHelper; 20 import com.cnlive.gundam.main.utils.SharedPreferencesHelper;
21 import com.cnlive.gundam.stream.model.ChannelInfoModel; 21 import com.cnlive.gundam.stream.model.ChannelInfoModel;
22 import com.cnlive.gundam.stream.ui.activity.StreamActivity; 22 import com.cnlive.gundam.stream.ui.activity.StreamActivity;
  23 +import com.cnlive.gundam.stream.util.FileUtil;
23 import com.cnlive.gundam.user.activity.LoginActivity; 24 import com.cnlive.gundam.user.activity.LoginActivity;
24 import com.cnlive.gundam.user.auth.UserService; 25 import com.cnlive.gundam.user.auth.UserService;
25 import com.cnlive.gundam.user.model.User; 26 import com.cnlive.gundam.user.model.User;
@@ -51,6 +52,9 @@ public class MainActivity extends AppCompatActivity implements FragmentTabHost.O @@ -51,6 +52,9 @@ public class MainActivity extends AppCompatActivity implements FragmentTabHost.O
51 )); 52 ));
52 53
53 mAnchorInfoPresenter = new AnchorInfoPresenter(this); 54 mAnchorInfoPresenter = new AnchorInfoPresenter(this);
  55 +
  56 + //加载水印图片
  57 + FileUtil.copyFile(this, "cnlive_logo.png");
54 } 58 }
55 59
56 @Override 60 @Override
@@ -106,6 +110,7 @@ public class MainActivity extends AppCompatActivity implements FragmentTabHost.O @@ -106,6 +110,7 @@ public class MainActivity extends AppCompatActivity implements FragmentTabHost.O
106 110
107 /** 111 /**
108 * 开启直播推流页面 112 * 开启直播推流页面
  113 + *
109 * @param info 主播信息 114 * @param info 主播信息
110 */ 115 */
111 private void startStreamActivity(ChannelInfoModel info) { 116 private void startStreamActivity(ChannelInfoModel info) {
@@ -113,7 +118,7 @@ public class MainActivity extends AppCompatActivity implements FragmentTabHost.O @@ -113,7 +118,7 @@ public class MainActivity extends AppCompatActivity implements FragmentTabHost.O
113 String channelId = info.getData().getChannelId(); 118 String channelId = info.getData().getChannelId();
114 String channelName = info.getData().getChannelName(); 119 String channelName = info.getData().getChannelName();
115 String coverImgUrl = info.getData().getCoverImgUrl(); 120 String coverImgUrl = info.getData().getCoverImgUrl();
116 - StreamActivity.start(this, activityId, channelId, channelName, coverImgUrl, false); 121 + StreamActivity.start(this, activityId, channelId, channelName, coverImgUrl);
117 } 122 }
118 123
119 /** 124 /**
app/src/main/java/com/cnlive/gundam/main/utils/AccelerometerSensor.java 0 → 100644
  1 +package com.cnlive.gundam.main.utils;
  2 +
  3 +import android.content.Context;
  4 +import android.content.pm.ActivityInfo;
  5 +import android.hardware.Sensor;
  6 +import android.hardware.SensorEvent;
  7 +import android.hardware.SensorEventListener;
  8 +import android.hardware.SensorManager;
  9 +
  10 +/**
  11 + * Created by xiansong on 2017/6/30.
  12 + */
  13 +
  14 +public class AccelerometerSensor {
  15 +
  16 + private static final int ORIENTATION_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
  17 + private static final int ORIENTATION_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
  18 + private static final int ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
  19 +
  20 + private Context context;
  21 + private SensorManager mSensorManager = null;
  22 + private ScreenOrientationListener listener;
  23 +
  24 + private int orientation = -1;
  25 + private double boundaryValue;
  26 +
  27 + public interface ScreenOrientationListener {
  28 + void onOrientationChange(int orientation);
  29 + }
  30 +
  31 + public AccelerometerSensor(Context context) {
  32 + this.context = context;
  33 + }
  34 +
  35 + public void unregister() {
  36 + mSensorManager.unregisterListener(lsn);
  37 + }
  38 +
  39 + public void register() {
  40 + boundaryValue = 8.5d;
  41 + mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
  42 + Sensor mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  43 + mSensorManager.registerListener(lsn, mSensor, SensorManager.SENSOR_DELAY_UI);
  44 + }
  45 +
  46 + private SensorEventListener lsn = new SensorEventListener() {
  47 +
  48 + @SuppressWarnings("deprecation")
  49 + @Override
  50 + public void onSensorChanged(SensorEvent event) {
  51 + double x = event.values[SensorManager.DATA_X];
  52 + double y = event.values[SensorManager.DATA_Y];
  53 + if (x > boundaryValue && orientation != ORIENTATION_LANDSCAPE) {
  54 + orientation_change(orientation = ORIENTATION_LANDSCAPE);
  55 + } else if (y > boundaryValue && orientation != ORIENTATION_PORTRAIT) {
  56 + orientation_change(orientation = ORIENTATION_PORTRAIT);
  57 + } else if (x < (-boundaryValue) && orientation != ORIENTATION_REVERSE_LANDSCAPE) {
  58 + orientation_change(orientation = ORIENTATION_REVERSE_LANDSCAPE);
  59 + }
  60 + }
  61 +
  62 + @Override
  63 + public void onAccuracyChanged(Sensor sensor, int accuracy) {
  64 +
  65 + }
  66 + };
  67 +
  68 + public int getOrientation() {
  69 + return orientation;
  70 + }
  71 +
  72 + public void setScreenOrientationListener(ScreenOrientationListener listener) {
  73 + this.listener = listener;
  74 + }
  75 +
  76 + private void orientation_change(int orientation) {
  77 + if (listener != null) listener.onOrientationChange(orientation);
  78 + }
  79 +
  80 +}
app/src/main/java/com/cnlive/gundam/stream/ui/activity/BaseStreamActivity.java
@@ -48,6 +48,8 @@ public class BaseStreamActivity extends AppCompatActivity implements Handler.Cal @@ -48,6 +48,8 @@ public class BaseStreamActivity extends AppCompatActivity implements Handler.Cal
48 48
49 private final static int PERMISSION_REQUEST_CAMERA_AUDIOREC = 1; 49 private final static int PERMISSION_REQUEST_CAMERA_AUDIOREC = 1;
50 50
  51 + private final static String path = Environment.getExternalStorageDirectory() + File.separator + "cnlive_logo.png";
  52 +
51 @Override 53 @Override
52 protected void onCreate(@Nullable Bundle savedInstanceState) { 54 protected void onCreate(@Nullable Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState); 55 super.onCreate(savedInstanceState);
@@ -55,10 +57,7 @@ public class BaseStreamActivity extends AppCompatActivity implements Handler.Cal @@ -55,10 +57,7 @@ public class BaseStreamActivity extends AppCompatActivity implements Handler.Cal
55 mHandler = new Handler(this); 57 mHandler = new Handler(this);
56 } 58 }
57 59
58 - protected void initStreamer(boolean landScape, GLSurfaceView glSurfaceView) {  
59 - setRequestedOrientation(landScape ?  
60 - ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE :  
61 - ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 60 + protected void initStreamer(GLSurfaceView glSurfaceView) {
62 mStreamer = new Streamer(this); 61 mStreamer = new Streamer(this);
63 //设置预览编码帧率 62 //设置预览编码帧率
64 mStreamer.setPreviewFps(15); 63 mStreamer.setPreviewFps(15);
@@ -75,19 +74,13 @@ public class BaseStreamActivity extends AppCompatActivity implements Handler.Cal @@ -75,19 +74,13 @@ public class BaseStreamActivity extends AppCompatActivity implements Handler.Cal
75 //设置编码模式 74 //设置编码模式
76 mStreamer.setEncodeMethod(IStreamer.ENCODE_METHOD_SOFTWARE_COMPAT); 75 mStreamer.setEncodeMethod(IStreamer.ENCODE_METHOD_SOFTWARE_COMPAT);
77 //设置横竖屏方向 76 //设置横竖屏方向
78 - mStreamer.setRotateDegrees(landScape ? 90 : 0);  
79 - if (landScape) {  
80 - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//横屏  
81 - } else {  
82 - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏  
83 - } 77 + mStreamer.setRotateDegrees(getOrientationAngle());
84 //设置预览view 78 //设置预览view
85 mStreamer.setDisplayPreview(glSurfaceView); 79 mStreamer.setDisplayPreview(glSurfaceView);
86 //设置前置摄像头镜像 80 //设置前置摄像头镜像
87 mStreamer.setFrontCameraMirror(true); 81 mStreamer.setFrontCameraMirror(true);
88 //设置水印 82 //设置水印
89 - String path = Environment.getExternalStorageDirectory() + File.separator + "cnlive_logo.png";  
90 - mStreamer.showWaterMarkLogo(path, 0.03f, 0.02f, landScape ? 0.0911f : 0.15f, landScape ? 0.0545f : 0.033f, 1f); 83 + mStreamer.showWaterMarkLogo(path, 0.03f, 0.02f, isLandscape() ? 0.0911f : 0.15f, isLandscape() ? 0.0545f : 0.033f, 1f);
91 //设置视频编码是的帧间隔,默认3秒 84 //设置视频编码是的帧间隔,默认3秒
92 mStreamer.setIFrameInterval(2); 85 mStreamer.setIFrameInterval(2);
93 //设置音频通道 86 //设置音频通道
@@ -100,6 +93,32 @@ public class BaseStreamActivity extends AppCompatActivity implements Handler.Cal @@ -100,6 +93,32 @@ public class BaseStreamActivity extends AppCompatActivity implements Handler.Cal
100 mStreamer.setOnErrorListener(mErrorListener); 93 mStreamer.setOnErrorListener(mErrorListener);
101 } 94 }
102 95
  96 + protected int getOrientationAngle() {
  97 + switch (getRequestedOrientation()) {
  98 + case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
  99 + return 0;
  100 + case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
  101 + return 90;
  102 + case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
  103 + return 270;
  104 + default:
  105 + return 0;
  106 + }
  107 + }
  108 +
  109 + protected boolean isLandscape() {
  110 + int angle = getOrientationAngle();
  111 + return angle == 90 || angle == 270;
  112 + }
  113 +
  114 + protected void orientationChange() {
  115 + //设置横竖屏方向
  116 + mStreamer.setRotateDegrees(getOrientationAngle());
  117 + //水印重绘
  118 + mStreamer.hideWaterMarkLogo();
  119 + mStreamer.showWaterMarkLogo(path, 0.03f, 0.02f, isLandscape() ? 0.0911f : 0.15f, isLandscape() ? 0.0545f : 0.033f, 1f);
  120 + }
  121 +
103 protected void switchCamera() { 122 protected void switchCamera() {
104 mStreamer.switchCamera(); 123 mStreamer.switchCamera();
105 } 124 }
app/src/main/java/com/cnlive/gundam/stream/ui/activity/StreamActivity.java
@@ -5,6 +5,7 @@ import android.app.Dialog; @@ -5,6 +5,7 @@ import android.app.Dialog;
5 import android.content.Context; 5 import android.content.Context;
6 import android.content.DialogInterface; 6 import android.content.DialogInterface;
7 import android.content.Intent; 7 import android.content.Intent;
  8 +import android.content.res.Configuration;
8 import android.databinding.DataBindingUtil; 9 import android.databinding.DataBindingUtil;
9 import android.os.Bundle; 10 import android.os.Bundle;
10 import android.os.Handler; 11 import android.os.Handler;
@@ -21,6 +22,7 @@ import android.widget.TextView; @@ -21,6 +22,7 @@ import android.widget.TextView;
21 22
22 import com.cnlive.gundam.R; 23 import com.cnlive.gundam.R;
23 import com.cnlive.gundam.databinding.ActivityStreamPortraitBinding; 24 import com.cnlive.gundam.databinding.ActivityStreamPortraitBinding;
  25 +import com.cnlive.gundam.main.utils.AccelerometerSensor;
24 import com.cnlive.gundam.stream.model.ChatItem; 26 import com.cnlive.gundam.stream.model.ChatItem;
25 import com.cnlive.gundam.stream.model.ExtensionModel; 27 import com.cnlive.gundam.stream.model.ExtensionModel;
26 import com.cnlive.gundam.stream.model.ShowView; 28 import com.cnlive.gundam.stream.model.ShowView;
@@ -41,7 +43,8 @@ import com.google.gson.Gson; @@ -41,7 +43,8 @@ import com.google.gson.Gson;
41 import java.util.ArrayList; 43 import java.util.ArrayList;
42 import java.util.List; 44 import java.util.List;
43 45
44 -public class StreamActivity extends BaseStreamActivity implements ChatConnect.ChatCallback, TimerUtil.TimerListener { 46 +public class StreamActivity extends BaseStreamActivity implements ChatConnect.ChatCallback,
  47 + TimerUtil.TimerListener, AccelerometerSensor.ScreenOrientationListener {
45 48
46 private boolean isFace = false; 49 private boolean isFace = false;
47 private boolean isFlash = false; 50 private boolean isFlash = false;
@@ -54,7 +57,6 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch @@ -54,7 +57,6 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch
54 private String channelId; 57 private String channelId;
55 private String channelName; 58 private String channelName;
56 private String coverImgUrl; 59 private String coverImgUrl;
57 - private boolean isLandscape = false;  
58 60
59 private Dialog endDialog; 61 private Dialog endDialog;
60 private ActivityStreamPortraitBinding binding; 62 private ActivityStreamPortraitBinding binding;
@@ -70,13 +72,14 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch @@ -70,13 +72,14 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch
70 private TimerUtil timerUtil; 72 private TimerUtil timerUtil;
71 private String extensions; 73 private String extensions;
72 74
73 - public static void start(Context context, String activityId, String channelId, String channelName, String coverImgUrl, boolean landscage) { 75 + private AccelerometerSensor accelerometerSensor;
  76 +
  77 + public static void start(Context context, String activityId, String channelId, String channelName, String coverImgUrl) {
74 Intent intent = new Intent(context, StreamActivity.class); 78 Intent intent = new Intent(context, StreamActivity.class);
75 intent.putExtra("activityId", activityId); 79 intent.putExtra("activityId", activityId);
76 intent.putExtra("channelId", channelId); 80 intent.putExtra("channelId", channelId);
77 intent.putExtra("channelName", channelName); 81 intent.putExtra("channelName", channelName);
78 intent.putExtra("coverImgUrl", coverImgUrl); 82 intent.putExtra("coverImgUrl", coverImgUrl);
79 - intent.putExtra("landscape", landscage);  
80 context.startActivity(intent); 83 context.startActivity(intent);
81 } 84 }
82 85
@@ -91,7 +94,7 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch @@ -91,7 +94,7 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch
91 initData(); 94 initData();
92 95
93 binding = DataBindingUtil.setContentView(this, R.layout.activity_stream_portrait); 96 binding = DataBindingUtil.setContentView(this, R.layout.activity_stream_portrait);
94 - initStreamer(isLandscape, binding.cameraPreview); 97 + initStreamer(binding.cameraPreview);
95 showView.setStartBtn(true); 98 showView.setStartBtn(true);
96 showView.setRetryLayout(false); 99 showView.setRetryLayout(false);
97 showView.setEndLayout(false); 100 showView.setEndLayout(false);
@@ -103,6 +106,21 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch @@ -103,6 +106,21 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch
103 chatItemList = new ArrayList<>(); 106 chatItemList = new ArrayList<>();
104 initChatRoomRecycel(); 107 initChatRoomRecycel();
105 timerUtil = new TimerUtil(this); 108 timerUtil = new TimerUtil(this);
  109 +
  110 + accelerometerSensor = new AccelerometerSensor(this);
  111 + accelerometerSensor.setScreenOrientationListener(this);
  112 + }
  113 +
  114 +
  115 + @Override
  116 + public void onOrientationChange(int orientation) {
  117 + setRequestedOrientation(orientation);
  118 + }
  119 +
  120 + @Override
  121 + public void onConfigurationChanged(Configuration newConfig) {
  122 + super.onConfigurationChanged(newConfig);
  123 + orientationChange();
106 } 124 }
107 125
108 private void initChatRoomRecycel() { 126 private void initChatRoomRecycel() {
@@ -117,7 +135,6 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch @@ -117,7 +135,6 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch
117 channelId = getIntent().getStringExtra("channelId"); 135 channelId = getIntent().getStringExtra("channelId");
118 channelName = getIntent().getStringExtra("channelName"); 136 channelName = getIntent().getStringExtra("channelName");
119 coverImgUrl = getIntent().getStringExtra("coverImgUrl"); 137 coverImgUrl = getIntent().getStringExtra("coverImgUrl");
120 - isLandscape = getIntent().getBooleanExtra("landscape", false);  
121 138
122 ExtensionModel extensionModel = new ExtensionModel(channelName, coverImgUrl); 139 ExtensionModel extensionModel = new ExtensionModel(channelName, coverImgUrl);
123 extensions = new Gson().toJson(extensionModel); 140 extensions = new Gson().toJson(extensionModel);
@@ -280,7 +297,7 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch @@ -280,7 +297,7 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch
280 297
281 @Override 298 @Override
282 public int getIsHorizontalScreen() { 299 public int getIsHorizontalScreen() {
283 - return isLandscape ? 1 : 0; 300 + return isLandscape() ? 1 : 0;
284 } 301 }
285 302
286 @Override 303 @Override
@@ -319,12 +336,14 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch @@ -319,12 +336,14 @@ public class StreamActivity extends BaseStreamActivity implements ChatConnect.Ch
319 super.onPause(); 336 super.onPause();
320 if (endDialog != null) endDialog.dismiss(); 337 if (endDialog != null) endDialog.dismiss();
321 if (timerUtil != null) timerUtil.cancelTimer(); 338 if (timerUtil != null) timerUtil.cancelTimer();
  339 + if (accelerometerSensor != null) accelerometerSensor.unregister();
322 } 340 }
323 341
324 @Override 342 @Override
325 protected void onResume() { 343 protected void onResume() {
326 super.onResume(); 344 super.onResume();
327 if (timerUtil != null) timerUtil.startTimer(0, TimerUtil.UPDATE_ONLINE_PERSON); 345 if (timerUtil != null) timerUtil.startTimer(0, TimerUtil.UPDATE_ONLINE_PERSON);
  346 + if (accelerometerSensor != null) accelerometerSensor.register();
328 } 347 }
329 348
330 @Override 349 @Override
app/src/main/java/com/cnlive/gundam/stream/util/FileUtil.java 0 → 100644
  1 +package com.cnlive.gundam.stream.util;
  2 +
  3 +import android.content.Context;
  4 +import android.content.res.AssetManager;
  5 +import android.os.Environment;
  6 +import android.util.Log;
  7 +
  8 +import java.io.FileOutputStream;
  9 +import java.io.InputStream;
  10 +import java.io.OutputStream;
  11 +
  12 +/**
  13 + * Created by xiansong on 2017/6/30.
  14 + */
  15 +
  16 +public class FileUtil {
  17 + public static void copyFile(Context context, String filename) {
  18 + AssetManager assetManager = context.getAssets();
  19 +
  20 + InputStream in = null;
  21 + OutputStream out = null;
  22 + try {
  23 + in = assetManager.open(filename);
  24 + String newFileName = Environment.getExternalStorageDirectory() + java.io.File.separator + filename;
  25 + out = new FileOutputStream(newFileName);
  26 +
  27 + byte[] buffer = new byte[1024];
  28 + int read;
  29 + while ((read = in.read(buffer)) != -1) {
  30 + out.write(buffer, 0, read);
  31 + }
  32 + in.close();
  33 + in = null;
  34 + out.flush();
  35 + out.close();
  36 + out = null;
  37 + } catch (Exception e) {
  38 + Log.e("tag", e.getMessage());
  39 + }
  40 + }
  41 +}
app/src/main/java/com/cnlive/gundam/user/fragment/LoginFragment.java
@@ -158,7 +158,7 @@ public class LoginFragment extends Fragment implements View.OnClickListener { @@ -158,7 +158,7 @@ public class LoginFragment extends Fragment implements View.OnClickListener {
158 UserService userService = UserService.getInstance(getActivity()); 158 UserService userService = UserService.getInstance(getActivity());
159 userService.setUserInfo(account,data); 159 userService.setUserInfo(account,data);
160 //userService.isAutoLogin(); 160 //userService.isAutoLogin();
161 - userService.autoLogin(binding.edtLoginNumber.toString(), binding.edtInputPassward.getText().toString(), data); 161 + userService.autoLogin(binding.edtLoginNumber.getText().toString(), binding.edtInputPassward.getText().toString(), data);
162 162
163 if (null != data.toString()) { 163 if (null != data.toString()) {
164 //getFragmentManager().popBackStack(); 164 //getFragmentManager().popBackStack();