Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Commit

Permalink
Added support for device settings (Screen brightness, wake lock and a…
Browse files Browse the repository at this point in the history
…uto connect)
  • Loading branch information
Manuel Mayer committed Jan 22, 2022
1 parent f25be74 commit 06a2e9b
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 44 deletions.
16 changes: 10 additions & 6 deletions Macro Deck Client.Android/AndroidBrightnessService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Android.Widget;
using Plugin.CurrentActivity;
using SuchByte.MacroDeck;
using SuchByte.MacroDeck.Droid;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -18,13 +19,16 @@ public class AndroidBrightnessService : IBrightnessService
{
public void SetBrightness(float brightness)
{
Log.Debug("Brightness", "value: " + brightness);
var window = CrossCurrentActivity.Current.Activity.Window;
var attributesWindow = new WindowManagerLayoutParams();
Device.BeginInvokeOnMainThread(() =>
{
Window window = AppInstance.MainActivity.Window;
var attributesWindow = new WindowManagerLayoutParams();

attributesWindow.CopyFrom(window.Attributes);
attributesWindow.ScreenBrightness = brightness;
attributesWindow.CopyFrom(window.Attributes);
attributesWindow.ScreenBrightness = brightness;

window.Attributes = attributesWindow;
window.Attributes = attributesWindow;
});

}
}
55 changes: 55 additions & 0 deletions Macro Deck Client.Android/AndroidWakeLockService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using SuchByte.MacroDeck;
using SuchByte.MacroDeck.Droid;
using SuchByte.MacroDeck.Settings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;

[assembly: Dependency(typeof(AndroidWakeLockService))]
public class AndroidWakeLockService : IWakeLockService
{
public string WakeLockMethod { get; set; } = "Connected";

public void SetWakeLock(bool state)
{
switch (WakeLockMethod)
{
case "Always":
state = true;
break;
case "Connected":
break;
case "Never":
state = false;
break;
}
AppInstance.MainActivity.WakeLockActive = state;
Log.Info("Wakelock", $"Set wakelock: { state }");
try
{
if (AppInstance.MainActivity.WakeLock == null) return; Device.BeginInvokeOnMainThread(() =>
{

if (state)
{
AppInstance.MainActivity.WakeLock.Acquire();
}
else
{
if (!AppInstance.MainActivity.WakeLock.IsHeld) return;
AppInstance.MainActivity.WakeLock.Release();
}
});
}
catch { }
}
}
35 changes: 31 additions & 4 deletions Macro Deck Client.Android/Assets/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var websocket;
var connected;
var recentConnections = [];
var clientId;
var rows = 3;
var columns = 5;
var buttonSpacing = 5;
var buttonRadius = 40;
var buttonSize = 100;
Expand All @@ -13,6 +15,7 @@ var deviceType = "";
var pressTimer;
var longPress = false;
var supportButtonReleaseLongPress = false;
var buttonsGenerated = false;

var apiVersion = 20;

Expand Down Expand Up @@ -52,7 +55,7 @@ function connect(url) {
}

document.location.hash = 'connecting';
document.getElementById("button-container").innerHTML = '<div class="d-flex align-items-center justify-content-center" style="height: 500px;"><h1>Connecting to ' + url + '... <span class="spinner-border spinner-border-lg" role="status" aria-hidden="true"></span></h1></div>';
document.getElementById("button-container").innerHTML = '<div class="d-flex align-items-center justify-content-center" style="height: 500px;"><h1>Connecting... <span class="spinner-border spinner-border-lg" role="status" aria-hidden="true"></span></h1></div>';

websocket = new WebSocket(url);

Expand All @@ -75,22 +78,44 @@ function connect(url) {
var obj = JSON.parse(e.data);
switch (obj.Method) {
case JsonMethod.GET_CONFIG:
columns = obj.Columns;
rows = obj.Rows;
buttonSpacing = obj.ButtonSpacing;
buttonRadius = obj.ButtonRadius;
buttonBackground = obj.ButtonBackground;
if (obj.SupportButtonReleaseLongPress && obj.SupportButtonReleaseLongPress == true) {
supportButtonReleaseLongPress = true;
}
brightness = obj.Brightness;
var settingsObj = { "Brightness": brightness }

var autoConnect = false;
if (obj.AutoConnect) {
autoConnect = obj.AutoConnect;
}

var wakeLock = "Connected";
if (obj.WakeLock) {
wakeLock = obj.WakeLock;
}

var settingsObj = { "Brightness": brightness, "AutoConnect": autoConnect, "WakeLock": wakeLock };
document.location.hash = 'connected;' + JSON.stringify(settingsObj);
generateGrid(obj.Columns, obj.Rows);
var jsonObj = { "Method": JsonMethod.GET_BUTTONS }

if (!buttonsGenerated) {
document.getElementById("button-container").innerHTML = '<div class="d-flex align-items-center justify-content-center" style="height: 500px;"><h1>Downloading icon packs and buttons... <span class="spinner-border spinner-border-lg" role="status" aria-hidden="true"></span></h1></div>';
}

var jsonObj = { "Method": JsonMethod.GET_BUTTONS };
doSend(JSON.stringify(jsonObj));


autoSize();
break;
case JsonMethod.GET_BUTTONS:
if (!buttonsGenerated) {
buttonsGenerated = true;
generateGrid(columns, rows);
}
var actionButtons = document.getElementsByClassName("action-button");
var labels = document.getElementsByClassName("label");
for (var i = 0; i < actionButtons.length; i++) {
Expand Down Expand Up @@ -322,6 +347,7 @@ function buttonPress(id) {
}, 1000);
if (document.getElementById("col_" + id)) {
document.getElementById("col_" + id).classList.toggle('pressed', true);
document.getElementById("col_" + id).classList.toggle('release-transition', false);
}
var jsonObj = { "Message": id, "Method": JsonMethod.BUTTON_PRESS }
doSend(JSON.stringify(jsonObj));
Expand All @@ -331,6 +357,7 @@ function buttonPressRelease(id) {
clearTimeout(pressTimer);
if (document.getElementById("col_" + id)) {
document.getElementById("col_" + id).classList.toggle('pressed', false);
document.getElementById("col_" + id).classList.toggle('release-transition', true);
}

var jsonObj = { "Message": id, "Method": JsonMethod.BUTTON_RELEASE }
Expand Down
9 changes: 7 additions & 2 deletions Macro Deck Client.Android/Assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ p, h1, h2, h3, h4, h5, h6, label {
transform: scale(0.9);
}

.release-transition {
transition: all .3s linear;
}



.button-row {
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
margin: 0!important;
padding: 0!important;
margin: 0 !important;
padding: 0 !important;
}

.blockBox {
Expand Down
3 changes: 2 additions & 1 deletion Macro Deck Client.Android/Macro Deck Client.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<AndroidSigningKeyAlias>SuchByte</AndroidSigningKeyAlias>
<AndroidSigningKeyPass>hugo1930</AndroidSigningKeyPass>
<EmbedAssembliesIntoApk>true</EmbedAssembliesIntoApk>
<AndroidPackageFormat>aab</AndroidPackageFormat>
<AndroidPackageFormat>apk</AndroidPackageFormat>
<AndroidUseAapt2>true</AndroidUseAapt2>
<AndroidCreatePackagePerAbi>false</AndroidCreatePackagePerAbi>
</PropertyGroup>
Expand All @@ -88,6 +88,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AndroidBrightnessService.cs" />
<Compile Include="AndroidWakeLockService.cs" />
<Compile Include="AppVersionProvider.cs" />
<Compile Include="MainActivity.cs" />
<Compile Include="Renderer\CustomEntityRenderer.cs" />
Expand Down
67 changes: 45 additions & 22 deletions Macro Deck Client.Android/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
[assembly: Dependency(typeof(BaseUrl_Android))]
namespace SuchByte.MacroDeck.Droid
{
public static class AppInstance
{
public static MainActivity MainActivity { get; set; }
}

public class BaseUrl_Android : IBaseUrl
{
public string Get()
Expand All @@ -25,10 +30,13 @@ public string Get()
[Activity(Label = "Macro Deck", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
private WakeLock wakeLock;
public WakeLock WakeLock;

public bool WakeLockActive = false;

public MainActivity()
{
AppInstance.MainActivity ??= this;
AppCompatDelegate.CompatVectorFromResourcesEnabled = true;
}

Expand All @@ -37,9 +45,15 @@ protected override void OnCreate(Bundle savedInstanceState)
base.OnCreate(savedInstanceState);

Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
Forms.Init(this, savedInstanceState);

LoadApplication(new App());

try
{
PowerManager powerManager = (PowerManager)this.GetSystemService(Context.PowerService);
WakeLock = powerManager.NewWakeLock(WakeLockFlags.Full, "Macro Deck");
} catch { }
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Expand All @@ -52,33 +66,24 @@ public override void OnRequestPermissionsResult(int requestCode, string[] permis
protected override void OnResume()
{
base.OnResume();
RemoveNavigation();
try
{
PowerManager powerManager = (PowerManager)this.GetSystemService(Context.PowerService);
wakeLock = powerManager.NewWakeLock(WakeLockFlags.Full, "Macro Deck");
wakeLock.Acquire();

this.Window.AddFlags(WindowManagerFlags.Fullscreen | WindowManagerFlags.TranslucentNavigation);

int uiOptions = (int)Window.DecorView.SystemUiVisibility;

uiOptions |= (int)SystemUiFlags.LowProfile;
uiOptions |= (int)SystemUiFlags.Fullscreen;
uiOptions |= (int)SystemUiFlags.HideNavigation;
uiOptions |= (int)SystemUiFlags.ImmersiveSticky;

Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;
} catch { }

if (WakeLockActive && WakeLock != null)
{
WakeLock.Acquire();
}
}
catch { }
}

protected override void OnDestroy()
{
base.OnDestroy();
try
{
if (wakeLock == null) return;
wakeLock.Release();
if (WakeLock == null || !WakeLock.IsHeld) return;
WakeLock.Release();
} catch { }
}

Expand All @@ -87,8 +92,26 @@ protected override void OnPause()
base.OnPause();
try
{
if (wakeLock == null) return;
wakeLock.Release();
if (WakeLock == null || !WakeLock.IsHeld) return;
WakeLock.Release();
}
catch { }
}

private void RemoveNavigation()
{
try
{
this.Window.AddFlags(WindowManagerFlags.Fullscreen | WindowManagerFlags.TranslucentNavigation);

int uiOptions = (int)Window.DecorView.SystemUiVisibility;

uiOptions |= (int)SystemUiFlags.LowProfile;
uiOptions |= (int)SystemUiFlags.Fullscreen;
uiOptions |= (int)SystemUiFlags.HideNavigation;
uiOptions |= (int)SystemUiFlags.ImmersiveSticky;

Window.DecorView.SystemUiVisibility = (StatusBarVisibility)uiOptions;
}
catch { }
}
Expand Down
2 changes: 1 addition & 1 deletion Macro Deck Client.Android/Properties/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="211" android:versionName="2.1.1-beta" package="com.suchbyte.macrodeck" android:installLocation="auto">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="220" android:versionName="2.2.0-beta" package="com.suchbyte.macrodeck" android:installLocation="auto">
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="30" />
<application android:label="Macro Deck" android:theme="@style/MainTheme" android:usesCleartextTraffic="true" android:icon="@mipmap/launcher_foreground"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand Down
13 changes: 13 additions & 0 deletions Macro Deck Client/IWakeLockService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using SuchByte.MacroDeck.Settings;
using System;
using System.Collections.Generic;
using System.Text;

namespace SuchByte.MacroDeck
{
public interface IWakeLockService
{
string WakeLockMethod { get; set; }
void SetWakeLock(bool state);
}
}
2 changes: 1 addition & 1 deletion Macro Deck Client/Macro Deck Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
<Version>2.1.1-beta</Version>
<Version>2.2.0-beta</Version>
<Authors>Manuel Mayer</Authors>
<ApplicationIcon>Macro Deck 2021.ico</ApplicationIcon>
<RootNamespace>SuchByte.MacroDeck</RootNamespace>
Expand Down
6 changes: 6 additions & 0 deletions Macro Deck Client/Pages/DeckPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public partial class DeckPage : ContentPage
public int _port;
public int Port { get { return this._port; } }

private string currentSettingsJson = "";

public event EventHandler<ConnectionStateChangedEventArgs> ConnectionStateChanged;
public event EventHandler<SettingsChangedEventArgs> SettingsChanged;

Expand All @@ -43,6 +45,7 @@ public DeckPage(MainPage mainPage)
{
this._mainPage = mainPage;
InitializeComponent();

}

public void Open(string host, int port)
Expand Down Expand Up @@ -97,6 +100,8 @@ private void WebView_Navigated(object sender, WebNavigatedEventArgs e)
if (newState.Split(';').Length > 0)
{
string settingsJson = newState.Split(';')[1];
if (currentSettingsJson.Equals(settingsJson)) return;
Debug.WriteLine("Settings: " + settingsJson);
ClientSettings clientSettings = JsonConvert.DeserializeObject<ClientSettings>(settingsJson);
if (SettingsChanged != null)
{
Expand All @@ -109,6 +114,7 @@ private void WebView_Navigated(object sender, WebNavigatedEventArgs e)

protected override bool OnBackButtonPressed()
{
this._mainPage.ManuallyDisconnected = true;
this.Close();
return true;
}
Expand Down
Loading

0 comments on commit 06a2e9b

Please sign in to comment.