-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
98 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
app/src/main/java/org/ostrya/presencepublisher/message/battery/BatteryMessageProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.ostrya.presencepublisher.message.battery; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.IntentFilter; | ||
import android.content.SharedPreferences; | ||
import android.os.BatteryManager; | ||
import androidx.preference.PreferenceManager; | ||
import com.hypertrack.hyperlog.HyperLog; | ||
import org.ostrya.presencepublisher.message.Message; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.ostrya.presencepublisher.ui.ScheduleFragment.BATTERY_MESSAGE; | ||
import static org.ostrya.presencepublisher.ui.ScheduleFragment.BATTERY_TOPIC; | ||
|
||
public class BatteryMessageProvider { | ||
private static final String TAG = "BatteryMessageProvider"; | ||
|
||
private final Context applicationContext; | ||
private final SharedPreferences sharedPreferences; | ||
|
||
public BatteryMessageProvider(Context context) { | ||
this.applicationContext = context.getApplicationContext(); | ||
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext); | ||
} | ||
|
||
public List<Message> getMessages() { | ||
if (!sharedPreferences.getBoolean(BATTERY_MESSAGE, false)) { | ||
HyperLog.d(TAG, "Battery messages disabled, not generating any messages"); | ||
return Collections.emptyList(); | ||
} | ||
String topic = sharedPreferences.getString(BATTERY_TOPIC, null); | ||
if (topic == null) { | ||
HyperLog.w(TAG, "No topic defined, not generating any messages"); | ||
return Collections.emptyList(); | ||
} | ||
|
||
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); | ||
Intent batteryStatus = applicationContext.registerReceiver(null, filter); | ||
|
||
if (batteryStatus == null) { | ||
HyperLog.w(TAG, "No battery status received, unable to generate message"); | ||
return Collections.emptyList(); | ||
} | ||
|
||
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); | ||
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); | ||
|
||
int batteryPct = (int) (level / (0.01f * scale)); | ||
|
||
return Collections.singletonList(Message.messageForTopic(topic) | ||
.withContent(Integer.toString(batteryPct))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters