Skip to content

Commit

Permalink
Merge pull request #16 from cagnulein/keep_aspect_ratio
Browse files Browse the repository at this point in the history
Keep aspect ratio
  • Loading branch information
cagnulein authored Jul 9, 2024
2 parents 788601d + 6fc54b0 commit 191bed2
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 87 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
applicationId "org.cagnulein.android_remote"
minSdkVersion 29
targetSdkVersion 32
versionCode 17
versionCode 19
versionName "2.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
setProperty("archivesBaseName", "android_remote")
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<activity
android:name=".MainActivity"
android:resizeableActivity="true"
android:launchMode="singleTask"
android:launchMode="singleTask"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
71 changes: 51 additions & 20 deletions app/src/main/java/org/cagnulein/android_remote/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;

import io.github.muntashirakon.adb.AbsAdbConnectionManager;
import io.github.muntashirakon.adb.AdbPairingRequiredException;
Expand Down Expand Up @@ -101,8 +102,9 @@ public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
scrcpy = ((Scrcpy.MyServiceBinder) iBinder).getService();
scrcpy.setServiceCallbacks(MainActivity.this);
serviceBound = true;
Log.d("screen", screenHeight + " " + screenWidth);
if (first_time) {
scrcpy.start(surface, serverAdr, screenHeight, screenWidth);
scrcpy.start(surface, serverAdr, screenWidth, screenHeight);
int count = 100;
while (count!=0 && !scrcpy.check_socket_connection()){
count --;
Expand All @@ -127,7 +129,7 @@ public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
first_time = false;
}
} else {
scrcpy.setParms(surface, screenWidth, screenHeight);
scrcpy.setParms(surface, screenHeight, screenWidth);
}
set_display_nd_touch();
}
Expand Down Expand Up @@ -319,12 +321,14 @@ public void get_saved_preferences(){
final Switch aSwitch0 = findViewById(R.id.switch0);
final Switch aSwitch1 = findViewById(R.id.switch1);
final Switch adebuglog = findViewById(R.id.debuglog);
final Switch keepaspectratio = findViewById(R.id.keep_aspect_ratio);
editTextServerHost.setText(context.getSharedPreferences(PREFERENCE_KEY, 0).getString("Server Address", ""));
editTextServerPort.setText(context.getSharedPreferences(PREFERENCE_KEY, 0).getString("Server Port", ""));
editTextPairPort.setText(context.getSharedPreferences(PREFERENCE_KEY, 0).getString("Pair Port", ""));
aSwitch0.setChecked(context.getSharedPreferences(PREFERENCE_KEY, 0).getBoolean("No Control", false));
aSwitch1.setChecked(context.getSharedPreferences(PREFERENCE_KEY, 0).getBoolean("Nav Switch", false));
adebuglog.setChecked(context.getSharedPreferences(PREFERENCE_KEY, 0).getBoolean("Debug Log", false));
keepaspectratio.setChecked(context.getSharedPreferences(PREFERENCE_KEY, 0).getBoolean("Keep Aspect Ratio", false));
setSpinner(R.array.options_resolution_keys, R.id.spinner_video_resolution, PREFERENCE_SPINNER_RESOLUTION);
setSpinner(R.array.options_bitrate_keys, R.id.spinner_video_bitrate, PREFERENCE_SPINNER_BITRATE);
if(aSwitch0.isChecked()){
Expand All @@ -345,6 +349,7 @@ public void get_saved_preferences(){

@SuppressLint("ClickableViewAccessibility")
public void set_display_nd_touch() {
final View decodeSurface = findViewById(R.id.decoder_surface);
DisplayMetrics metrics = new DisplayMetrics();
if (ViewConfiguration.get(context).hasPermanentMenuKey()) {
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Expand All @@ -356,30 +361,49 @@ public void set_display_nd_touch() {
float this_dev_width = metrics.widthPixels;
if (nav && !no_control){
if (landscape){
this_dev_width = this_dev_width - 96;
//this_dev_width = this_dev_width - 96;
}else { //100 is the height of nav bar but need multiples of 8.
this_dev_height = this_dev_height - 96;
//this_dev_height = this_dev_height - 96;
}
}
float remote_device_aspect_ratio = remote_device_height/remote_device_width;
/*
if (!landscape) { //Portrait
float this_device_aspect_ratio = this_dev_height/this_dev_width;
if (remote_device_aspect_ratio > this_device_aspect_ratio) {
linearLayout.setPadding((int) (((remote_device_aspect_ratio - this_device_aspect_ratio)*this_dev_width)/2),0,(int) (((remote_device_aspect_ratio - this_device_aspect_ratio)*this_dev_width)/2),0);
} else if (remote_device_aspect_ratio < this_device_aspect_ratio) {
linearLayout.setPadding(0,(int) (((this_device_aspect_ratio - remote_device_aspect_ratio)*this_dev_width)),0,0);
}
float this_device_aspect_ratio;
int padding = 0;
float remote_device_aspect_ratio = 0;
if(context.getSharedPreferences(PREFERENCE_KEY, 0).getBoolean("Keep Aspect Ratio", false)) {
if (!landscape) {
//Portrait
remote_device_aspect_ratio = remote_device_height/remote_device_width;
this_device_aspect_ratio = this_dev_height/this_dev_width;
if (remote_device_aspect_ratio > this_device_aspect_ratio) {
padding = (int)((this_dev_width - (this_dev_height / remote_device_aspect_ratio)) / 2);
if(padding >= 0)
linearLayout.setPadding(padding, 0, padding, 0);
} else if (remote_device_aspect_ratio < this_device_aspect_ratio) {
padding = (int)((this_dev_height - (this_dev_width * remote_device_aspect_ratio)) / 2);
if(padding >= 0)
linearLayout.setPadding(0, padding, 0, padding);
}

}else{ //Landscape
float this_device_aspect_ratio = this_dev_width/this_dev_height;
if (remote_device_aspect_ratio > this_device_aspect_ratio) {
linearLayout.setPadding(0,(int) (((remote_device_aspect_ratio - this_device_aspect_ratio)*this_dev_height)/2),0,(int) (((remote_device_aspect_ratio - this_device_aspect_ratio)*this_dev_height)/2));
} else if (remote_device_aspect_ratio < this_device_aspect_ratio) {
linearLayout.setPadding(((int) (((this_device_aspect_ratio - remote_device_aspect_ratio)*this_dev_height))/2),0,((int) (((this_device_aspect_ratio - remote_device_aspect_ratio)*this_dev_height))/2),0);
}else{
//Landscape
remote_device_aspect_ratio = remote_device_width/remote_device_height;
this_device_aspect_ratio = this_dev_height/this_dev_width;
if (remote_device_aspect_ratio > this_device_aspect_ratio) {
padding = (int)((this_dev_width - (this_dev_height / remote_device_aspect_ratio)) / 2);
if(padding >= 0)
linearLayout.setPadding(padding, 0, padding, 0);
} else if (remote_device_aspect_ratio < this_device_aspect_ratio) {
padding = (int)((this_dev_height - (this_dev_width * remote_device_aspect_ratio)) / 2);
if(padding >= 0)
linearLayout.setPadding(0, padding, 0, padding);
}
}
if(padding < 0)
linearLayout.setPadding(0, 0, 0, 0);

Log.d("aspect_ratio ", landscape + " " + remote_device_aspect_ratio + " " + this_device_aspect_ratio + " " + this_dev_width + " " + this_dev_height + " " + padding + " " + remote_device_width + " " + remote_device_height + " " + screenWidth + " " + screenHeight);
}

}*/
if (!no_control) {
surfaceView.setOnTouchListener((v, event) -> scrcpy.touchevent(event, surfaceView.getWidth(), surfaceView.getHeight(), landscape));
}
Expand Down Expand Up @@ -438,9 +462,11 @@ private void getAttributes() {
final Switch a_Switch1 = findViewById(R.id.switch1);
nav = a_Switch1.isChecked();
final Switch a_debuglog = findViewById(R.id.debuglog);
final Switch keepaspectratio = findViewById(R.id.keep_aspect_ratio);
context.getSharedPreferences(PREFERENCE_KEY, 0).edit().putBoolean("No Control", no_control).apply();
context.getSharedPreferences(PREFERENCE_KEY, 0).edit().putBoolean("Nav Switch", nav).apply();
context.getSharedPreferences(PREFERENCE_KEY, 0).edit().putBoolean("Debug Log", a_debuglog.isChecked()).apply();
context.getSharedPreferences(PREFERENCE_KEY, 0).edit().putBoolean("Keep Aspect Ratio", keepaspectratio.isChecked()).apply();

final String[] videoResolutions = getResources().getStringArray(R.array.options_resolution_values)[videoResolutionSpinner.getSelectedItemPosition()].split(",");
screenHeight = Integer.parseInt(videoResolutions[0]);
Expand Down Expand Up @@ -517,6 +543,7 @@ private void start_Scrcpy_service() {
@SuppressLint("SourceLockedOrientationActivity")
@Override
public void loadNewRotation() {
Log.d("aspect_ratio", "loadNewRotation");
if (first_time){
int[] rem_res = scrcpy.get_remote_device_resolution();
remote_device_height = rem_res[1];
Expand All @@ -528,6 +555,10 @@ public void loadNewRotation() {
result_of_Rotation = true;
landscape = !landscape;
swapDimensions();
runOnUiThread(() -> {
set_display_nd_touch();
});

/*if (landscape) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
Expand Down
65 changes: 0 additions & 65 deletions app/src/main/res/layout-land/surface.xml

This file was deleted.

16 changes: 16 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,24 @@
android:layout_height="wrap_content"
android:entries="@array/options_bitrate_keys"
android:minHeight="48dp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">

<Switch
android:id="@+id/keep_aspect_ratio"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:text="Keep Aspect Ratio"
android:textSize="15sp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
Expand Down

0 comments on commit 191bed2

Please sign in to comment.