Skip to content

Commit

Permalink
lmi: parts: Introduce Refresh Rate QS tile
Browse files Browse the repository at this point in the history
  • Loading branch information
adithya2306 authored and macka69 committed Jul 8, 2022
1 parent 4edd6cb commit 27c69f5
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
11 changes: 11 additions & 0 deletions parts/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,16 @@
android:name="com.android.settings.summary"
android:resource="@string/htsr_enable_summary" />
</activity>

<service
android:name=".RefreshRateTileService"
android:icon="@drawable/ic_qs_refresh_rate"
android:label="@string/refresh_rate_tile_title"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE"/>
</intent-filter>
</service>

</application>
</manifest>
14 changes: 14 additions & 0 deletions parts/res/drawable/ic_qs_refresh_rate.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?android:attr/colorControlNormal">
<path
android:fillColor="#ff000000"
android:pathData="M17 1.01L7 1C5.9 1 5 1.9 5 3v18c0 1.1 0.9 2 2 2h10c1.1 0 2-0.9 2-2V3c0-1.1-0.9-1.99-2-1.99zM17 21H7V3h10v18z"/>
<path
android:fillColor="#ff000000"
android:pathData="M12 8c1.1 0 2.1 0.4 2.8 1.2L16 8v4h-4l1.8-1.8c-0.5-0.4-1.1-0.7-1.8-0.7c-1.4 0-2.5 1.1-2.5 2.5s1.1 2.5 2.5 2.5c0.8 0 1.5-0.4 2-1h1.7C15.1 15 13.7 16 12 16c-2.2 0-4-1.8-4-4s1.8-4 4-4z"/>
</vector>
3 changes: 3 additions & 0 deletions parts/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,7 @@
<!-- High Touch Polling Rate Support strings -->
<string name="htsr_enable_title">Increase Touch Responsiveness</string>
<string name="htsr_enable_summary">Increases touch polling rate to decrease latency</string>

<!-- Refresh Rate Settings -->
<string name="refresh_rate_tile_title">Refresh Rate</string>
</resources>
106 changes: 106 additions & 0 deletions parts/src/org/lineageos/settings/RefreshRateTileService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (C) 2021 crDroid Android Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.lineageos.settings;

import android.content.Context;
import android.provider.Settings;
import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService;
import android.view.Display;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class RefreshRateTileService extends TileService {
private static final String KEY_MIN_REFRESH_RATE = "min_refresh_rate";
private static final String KEY_PEAK_REFRESH_RATE = "peak_refresh_rate";

private Context context;
private Tile tile;

private final List<Integer> availableRates = new ArrayList<>();
private int activeRateMin;
private int activeRateMax;

@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
Display.Mode mode = context.getDisplay().getMode();
Display.Mode[] modes = context.getDisplay().getSupportedModes();
for (Display.Mode m : modes) {
int rate = (int) Math.round(m.getRefreshRate());
if (m.getPhysicalWidth() == mode.getPhysicalWidth() &&
m.getPhysicalHeight() == mode.getPhysicalHeight()) {
availableRates.add(rate);
}
}
syncFromSettings();
}

private int getSettingOf(String key) {
float rate = Settings.System.getFloat(context.getContentResolver(), key, 60);
int active = availableRates.indexOf((int) Math.round(rate));
return Math.max(active, 0);
}

private void syncFromSettings() {
activeRateMin = getSettingOf(KEY_MIN_REFRESH_RATE);
activeRateMax = getSettingOf(KEY_PEAK_REFRESH_RATE);
}

private void cycleRefreshRate() {
if (activeRateMin < availableRates.size() - 1) {
activeRateMin++;
} else {
activeRateMin = 0;
}

float rate = availableRates.get(activeRateMin);
Settings.System.putFloat(context.getContentResolver(), KEY_MIN_REFRESH_RATE, rate);
Settings.System.putFloat(context.getContentResolver(), KEY_PEAK_REFRESH_RATE, rate);
}

private void updateTileView() {
String displayText;
int min = availableRates.get(activeRateMin);
int max = availableRates.get(activeRateMax);

displayText = String.format(Locale.US, min == max ? "%d Hz" : "%d - %d Hz", min, max);
tile.setContentDescription(displayText);
tile.setSubtitle(displayText);
tile.setState(min == max ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
tile.updateTile();
}

@Override
public void onStartListening() {
super.onStartListening();
tile = getQsTile();
syncFromSettings();
updateTileView();
}

@Override
public void onClick() {
super.onClick();
cycleRefreshRate();
syncFromSettings();
updateTileView();
}
}

0 comments on commit 27c69f5

Please sign in to comment.