package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.Toast;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
public static String TAG ="MainActivity";
Vibrator mVibrator;
private SeekBar seekBar;
private SharedPreferences shared = null;
private int num=0;
//实例化的WindowManager.
WindowManager windowManager;
View eyeView;
//振动,三段式
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mVibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
Switch sc2 = findViewById(R.id.switch2);//升降
sc2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//升降com.android.server.camera.CameraServiceProxy
if (isChecked) {
//Toast.makeText(getApplicationContext(),"上", Toast.LENGTH_SHORT).show();
writeLine("/sys/devices/platform/vendor/vendor:motor_pl/direction", "1");
writeLine("/sys/devices/platform/vendor/vendor:motor_pl/enable", "1");
} else {
writeLine("/sys/devices/platform/vendor/vendor:motor_pl/direction", "0");
writeLine("/sys/devices/platform/vendor/vendor:motor_pl/enable", "1");
}
}
});
Switch sc = findViewById(R.id.switch1);//双击亮屏
sc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
//双击亮屏com.android.server.input.InputManagerService
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
writeLine("/proc/touchpanel/double_tap_enable", "1");
} else {
writeLine("/proc/touchpanel/double_tap_enable", "0");
}
}
});
Switch sc3 = findViewById(R.id.switch3);//最高亮度
sc3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
//双击亮屏com.android.server.input.InputManagerService
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
writeLine("/sys/class/drm/card0-DSI-1/hbm", "5");
} else {
writeLine("/sys/class/drm/card0-DSI-1/hbm", "0");
}
}
});
Switch sc4 = findViewById(R.id.switch4);//dc模式
sc4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
//双击亮屏com.android.server.input.InputManagerService
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
writeLine("/sys/class/drm/card0-DSI-1/dimlayer_bl_en", "1");
} else {
writeLine("/sys/class/drm/card0-DSI-1/dimlayer_bl_en", "0");
}
}
});
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vibrateChenExit();
removeFullScreenWin();
int i=255*(getScreenBrightness())/4096;
//addFullScreenWin(Math.round(i*0.5));
Toast.makeText(getApplicationContext(), String.valueOf(getScreenBrightness()), Toast.LENGTH_SHORT).show();
}
});
//自定义亮度dataOutputStream.writeBytes("echo " + progress + " > /sys/class/backlight/panel0-backlight/brightness\n");
//阳光屏dataOutputStream.writeBytes("echo 1 > /sys/devices/virtual/graphics/fb0/hbm\n");
//三段式com.android.server.policy.PhoneWindowManager
seekBar = (SeekBar) findViewById(R.id.seekBar1);
shared = getSharedPreferences("base64", MODE_PRIVATE);
num = shared.getInt("seekBarNum", 0);
//changeAppBrightness(num);
seekBar.setProgress(num);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
/* 结束 */
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
/* 开始 */
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// TODO Auto-generated method stub
//changeAppBrightness(seekBar.getProgress());
}
});
if (Build.VERSION.SDK_INT >= 23) {
if (Settings.canDrawOverlays(MainActivity.this)) {
} else {
//若没有权限,提示获取.
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
Toast.makeText(MainActivity.this, "需要取得权限以使用悬浮窗", Toast.LENGTH_SHORT).show();
startActivity(intent);
}
}
}
public void addFullScreenWin(long n) {
if(windowManager == null) {
windowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
}
eyeView = new View(getApplicationContext());
//eyeView.setBackgroundColor(Util.getColor(20));
eyeView.setBackgroundColor(Color.argb(n,0,0,0));
final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
int flag=WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL| WindowManager.LayoutParams.FLAG_FULLSCREEN|
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS|
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
params.format = PixelFormat.TRANSLUCENT;
params.flags = flag;
Display display = windowManager.getDefaultDisplay();
Point p = new Point();
display.getRealSize(p);
params.width = p.x*4;
params.height = p.y*2;
if(android.os.Build.VERSION.SDK_INT >= 26) {
params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
params.type = WindowManager.LayoutParams.TYPE_PHONE;
}
windowManager.addView(eyeView, params);
}
public void removeFullScreenWin() {
if(windowManager!=null && eyeView!=null) {
windowManager.removeView(eyeView);
eyeView = null;
}
}
/**
* 获得当前亮度
*/
public int getScreenBrightness() {
int value = 0;
ContentResolver cr = getContentResolver();
try {
value = Settings.System.getInt(cr, Settings.System.SCREEN_BRIGHTNESS);
} catch (Settings.SettingNotFoundException e) {
}
return value;
}
// 获取app亮度
public void changeAppBrightness(int brightness) {
Window window = getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = (brightness <= 0 ? 1 : brightness) / 255f;
window.setAttributes(lp);
}
public void vibrateChen() {
Vibrator vibrator = this.mVibrator;
for (int i = 0; i < 1; i++) {
vibrator.vibrate(VibrationEffect.createWaveform(new long[]{20}, new int[]{255}, -1));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void vibrateChenExit() {
Vibrator vibrator = this.mVibrator;
for (int i = 0; i < 2; i++) {
vibrator.vibrate(VibrationEffect.createWaveform(new long[]{0,10,1,10,1,10}, -1));
//vibrator.vibrate(VibrationEffect.createWaveform(new long[]{0,10,1,0,1,10}, -1));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static boolean writeLine(String fileName, String value) {
try {
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(value.getBytes());
fos.flush();
fos.close();
return true;
} catch (IOException e) {
Log.e(TAG, "Could not write to file " + fileName, e);
return false;
}
}
}