Skip to content

Commit

Permalink
Merge pull request #17 from isalgueiro/battery-info
Browse files Browse the repository at this point in the history
Add a setting to display battery information
  • Loading branch information
alescdb committed Dec 26, 2020
2 parents 8cf33ab + b50a0cb commit 4a1b527
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 2 deletions.
9 changes: 9 additions & 0 deletions launcher/src/main/java/org/cosinus/launchertv/Setup.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ public boolean showDate() {
return (true);
}

public boolean showBattery() {
try {
return (getPreferences().getBoolean(Preferences.PREFERENCE_SHOW_BATTERY, false));
} catch (Exception e) {
e.printStackTrace();
}
return (false);
}

public boolean showNames() {
try {
return (getPreferences().getBoolean(Preferences.PREFERENCE_SHOW_NAME, true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class Preferences extends PreferenceActivity {
public static final String PREFERENCE_TRANSPARENCY = "preference_transparency";
public static final String PREFERENCE_SCREEN_ON = "preference_screen_always_on";
public static final String PREFERENCE_SHOW_DATE = "preference_show_date";
public static final String PREFERENCE_SHOW_BATTERY = "preference_show_battery";
public static final String PREFERENCE_GRID_X = "preference_grid_x";
public static final String PREFERENCE_GRID_Y = "preference_grid_y";
public static final String PREFERENCE_SHOW_NAME = "preference_show_name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
package org.cosinus.launchertv.fragments;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
Expand All @@ -32,6 +35,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
Expand Down Expand Up @@ -60,6 +64,20 @@ public class ApplicationFragment extends Fragment implements View.OnClickListene
private TextView mDate;
private DateFormat mTimeFormat;
private DateFormat mDateFormat;
private TextView mBatteryLevel;
private ImageView mBatteryIcon;
private BroadcastReceiver mBatteryChangedReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
mBatteryLevel.setText(
String.format(getResources().getString(R.string.battery_level_text), level)
);
final int batteryIconId = intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, 0);
mBatteryIcon.setImageDrawable(getResources().getDrawable(batteryIconId));
}
};
private boolean mBatteryChangedReceiverRegistered = false;

private final Handler mHandler = new Handler();
private final Runnable mTimerTick = new Runnable() {
Expand Down Expand Up @@ -96,6 +114,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
mGridView = view.findViewById(R.id.application_grid);
mClock = (TextView) view.findViewById(R.id.clock);
mDate = (TextView) view.findViewById(R.id.date);
final LinearLayout batteryLayout = (LinearLayout) view.findViewById(R.id.battery_layout);
mBatteryLevel = (TextView) view.findViewById(R.id.battery_level);
mBatteryIcon = (ImageView) view.findViewById(R.id.battery_icon);

mTimeFormat = android.text.format.DateFormat.getTimeFormat(getActivity());
mDateFormat = android.text.format.DateFormat.getLongDateFormat(getActivity());
Expand All @@ -106,6 +127,18 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
if (mSetup.showDate() == false)
mDate.setVisibility(View.GONE);

if (mSetup.showBattery()) {
batteryLayout.setVisibility(View.VISIBLE);
getActivity().registerReceiver(this.mBatteryChangedReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
mBatteryChangedReceiverRegistered = true;
} else {
batteryLayout.setVisibility(View.INVISIBLE);
if (mBatteryChangedReceiverRegistered) {
getActivity().unregisterReceiver(this.mBatteryChangedReceiver);
mBatteryChangedReceiverRegistered = false;
}
}

mSettings.setOnClickListener(this);
mGridView.setOnClickListener(this);

Expand Down Expand Up @@ -229,6 +262,10 @@ private void updateApplications() {


private void restartActivity() {
if (mBatteryChangedReceiverRegistered) {
getActivity().unregisterReceiver(mBatteryChangedReceiver);
mBatteryChangedReceiverRegistered = false;
}
Intent intent = getActivity().getIntent();
getActivity().finish();
startActivity(intent);
Expand Down Expand Up @@ -273,13 +310,20 @@ private void setApplication(PackageManager pm, ApplicationView app, String packa
public void onStart() {
super.onStart();
setClock();
if (mSetup.showBattery() && !mBatteryChangedReceiverRegistered) {
getActivity().registerReceiver(this.mBatteryChangedReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
mBatteryChangedReceiverRegistered = true;
}
mHandler.postDelayed(mTimerTick, 1000);
}

@Override
public void onPause() {
super.onPause();
mHandler.removeCallbacks(mTimerTick);
if (mBatteryChangedReceiverRegistered) {
getActivity().unregisterReceiver(this.mBatteryChangedReceiver);
}
}

private void setClock() {
Expand Down
48 changes: 46 additions & 2 deletions launcher/src/main/res/layout/fragment_application.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,61 @@
android:src="@drawable/ic_apps"
tools:ignore="ContentDescription"/>


<LinearLayout
android:layout_width="0dp"
android:id="@+id/battery_layout"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:orientation="vertical"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="right">
<TextView
android:id="@+id/battery_level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:shadowColor="#ff000000"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="0.5"
android:text="@string/test_battery"
android:textColor="#ffffffff"
android:textSize="25sp"/>

<ImageView
android:id="@+id/battery_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingEnd="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_marginTop="5dp"/>

</LinearLayout>
</LinearLayout>


<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0"
android:orientation="vertical"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">

<TextView
android:id="@+id/clock"
android:layout_width="wrap_content"
Expand Down
2 changes: 2 additions & 0 deletions launcher/src/main/res/values-cs/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@
<string name="home_locked">Icon is locked by settings</string>
<string name="summary_locked">Prevent icons to be accidentally removed</string>
<string name="title_locked">Lock icons</string>
<string name="title_show_battery_information">Zobrazit informace o baterii</string>
<string name="summary_show_battery_information">Zobrazit stav baterie a stav na domovské obrazovce</string>
</resources>
2 changes: 2 additions & 0 deletions launcher/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@
<string name="home_locked">Icone verrouillée (cf. Préférences)</string>
<string name="summary_locked">Empêcher la suppression accidentelle des icones</string>
<string name="title_locked">Verrouiller les icones</string>
<string name="title_show_battery_information">Afficher les informations sur la batterie</string>
<string name="summary_show_battery_information">Afficher le niveau et l\'état de la batterie sur l\'écran d\'accueil</string>
</resources>
2 changes: 2 additions & 0 deletions launcher/src/main/res/values-sk/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@
<string name="home_locked">Icon is locked by settings</string>
<string name="summary_locked">Prevent icons to be accidentally removed</string>
<string name="title_locked">Lock icons</string>
<string name="title_show_battery_information">Zobraziť informácie o batérii</string>
<string name="summary_show_battery_information">Zobraziť stav batérie a stav na domovskej obrazovke</string>
</resources>
1 change: 1 addition & 0 deletions launcher/src/main/res/values/notrans.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<string name="app_name" translatable="false">Simple TV Launcher</string>
<string name="title_google_plus" translatable="false">Google+</string>
<string name="test_clock" translatable="false">00:00</string>
<string name="test_battery" translatable="false">42%</string>
<string name="test_date" translatable="false">Sunday 1, January</string>
<string name="test_application" translatable="false">Application</string>
<string name="transparency_default" translatable="false">0.4</string>
Expand Down
3 changes: 3 additions & 0 deletions launcher/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
<string name="title_system">System settings</string>
<string name="summary_system">Open system settings</string>
<string name="summary_show_date">Display the date bellow time</string>
<string name="summary_show_battery_information">Display battery level and status in home screen</string>
<string name="title_show_date">Display date</string>
<string name="title_show_battery_information">Display battery information</string>
<string name="battery_level_text" translatable="false">%1d%%</string>
<string name="summary_show_name">Display applications names on main screen</string>
<string name="title_show_name">Display names</string>
<string name="title_margin_x">Icons margins X</string>
Expand Down
5 changes: 5 additions & 0 deletions launcher/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
android:key="preference_show_date"
android:summary="@string/summary_show_date"
android:title="@string/title_show_date"/>
<CheckBoxPreference
android:defaultValue="false"
android:key="preference_show_battery"
android:summary="@string/summary_show_battery_information"
android:title="@string/title_show_battery_information"/>
<CheckBoxPreference
android:defaultValue="true"
android:key="preference_show_name"
Expand Down

0 comments on commit 4a1b527

Please sign in to comment.