Skip to content

Commit

Permalink
add onConfigurationChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
Yang Zhou committed Jul 12, 2016
1 parent dac92c7 commit d2ee9b0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 14 deletions.
17 changes: 9 additions & 8 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
Expand All @@ -80,6 +72,14 @@
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/builds" />
Expand All @@ -102,6 +102,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" />
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>

<uses-feature
android:name="android.hardware.bluetooth_le"
Expand All @@ -17,7 +18,8 @@
android:theme="@style/AppTheme" >
<activity
android:name=".ScanDeviceActivity"
android:label="@string/app_name" >
android:label="@string/app_name"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
44 changes: 44 additions & 0 deletions app/src/main/java/org/esec/mcg/bleinsight/ScanDeviceActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import android.bluetooth.le.ScanResult;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
Expand Down Expand Up @@ -39,8 +41,11 @@ public class ScanDeviceActivity extends Activity implements ScanDeviceUiCallback
private RecyclerView mRecyclerView;
private TextView scanToggle;

// private boolean isConfigurationChanged = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
LogUtils.e("onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_scan);

Expand Down Expand Up @@ -96,8 +101,33 @@ public void onDismiss(DialogInterface dialog) {
}
}

@Override
protected void onStart() {
LogUtils.e("onStart");
super.onStart();
}

@Override
protected void onStop() {
LogUtils.e("onStop");
super.onStop();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {

// 当屏幕布局模式为横屏时
// if (newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
// || newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
// isConfigurationChanged = true;
// }
LogUtils.e("onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}

@Override
protected void onResume() {
LogUtils.e("onResume");
super.onResume();

if (mBLEWrapper.isBtEnabled() == false) {
Expand All @@ -115,6 +145,7 @@ protected void onResume() {

@Override
protected void onPause() {
LogUtils.e("onPause");
super.onPause();
stopScanningInit();
// mBLEWrapper.stopScanning();
Expand All @@ -127,6 +158,18 @@ protected void onDestroy() {
super.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
LogUtils.e("onSaveInstanceState");
super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
LogUtils.e("onRestoreInstanceState");
super.onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ENABLE_BT_REQUEST_ID) {
Expand All @@ -147,6 +190,7 @@ public void startScanningInit() {
mScanning = true;
mScanDeviceAdapter.clearList();
mBLEWrapper.startScanning(SCANNING_TIMEOUT);

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ public ItemSlideHelper(Context context, Callback callback) {
mMaxVelocity = configuration.getScaledMaximumFlingVelocity();
}

//这里采用的是外部拦截法
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
Log.d("onInterceptTouchEvent", "MotionEvent: " + e.getAction());
Log.e("onInterceptTouchEvent", "MotionEvent: " + e.getAction());

int action = MotionEventCompat.getActionMasked(e);
// 这里获得的是点击点相对于容器的坐标,这里的容器是recyclerView而不是其中每一个item
int x = (int) e.getX();
int y = (int) e.getY();

// 如果 RecyclerView 滚动状态不是空闲 targetView 不是空
// 如果 RecyclerView 处于滚动状态 targetView 不是空
if (rv.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
if (mTargetView != null) {
// 将已展开的 item 折叠起来
Expand All @@ -79,10 +80,13 @@ public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

/**
* 如果之前有一个已经展开的 item,当此次点击事件没有发生在右侧的菜单中则返回 true,
* 对 ACTION_DOWN 进行拦截,
* 如果点击的是右侧菜单那么返回 false,这样做的原因是菜单需要响应 onClick
*/
if (mTargetView != null) {
return !inView(x, y);
boolean tmp = !inView(x, y);
Log.e("onInterceptTouchEvent", "!inView(x, y) = " + tmp);
return tmp;
}

// 查找需要显示菜单的 View;
Expand All @@ -96,6 +100,7 @@ public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

// 不是左右滑动,不拦截事件
if (Math.abs(deltaY) > Math.abs(deltaX)) {
Log.e("onInterceptTouchEvent", "????");
return false;
}

Expand All @@ -104,6 +109,7 @@ public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
break;

case MotionEvent.ACTION_CANCEL:
Log.e("onInterceptTouchEvent", "ACTION_CANCEL");
case MotionEvent.ACTION_UP:
Log.e("onInterceptTouchEvent", "ACTION_UP");
/**
Expand All @@ -130,7 +136,7 @@ public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
Log.d("onTouchEvent", "" + e.getAction());
Log.e("onTouchEvent", "" + e.getAction());

if (mExpandAndCollapseAnim != null && mExpandAndCollapseAnim.isRunning() || mTargetView == null) {
return;
Expand Down Expand Up @@ -174,7 +180,7 @@ public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}

/**
* 根据 targetView 的 scrollX 计算出 targetView 的便宜,这样能够知道这个 point 是在右侧的菜单中
* 根据 targetView 的 scrollX 计算出 targetView 的偏移,这样能够知道这个 point 是在右侧的菜单中
* @param x
* @param y
* @return 是否在右侧的菜单中
Expand Down Expand Up @@ -307,10 +313,19 @@ public void clearState() {
}

public interface Callback {
/**
* 获得右侧菜单的宽度,这里是100dp
*/
int getHorizontalRange(RecyclerView.ViewHolder holder);

/**
* 通过 View 获得它所对应的在 RecyclerView 中的 ViewHolder
*/
RecyclerView.ViewHolder getChildViewHolder(View childView);

/**
* 通过点击的坐标获得所点击的 View
*/
View findTargetView(float x, float y);
}
}

0 comments on commit d2ee9b0

Please sign in to comment.