├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── xml │ │ │ │ ├── backup_descriptor.xml │ │ │ │ └── settings.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── values │ │ │ │ ├── colors.xml │ │ │ │ ├── strings.xml │ │ │ │ └── styles.xml │ │ │ ├── menu │ │ │ │ └── menu_main.xml │ │ │ ├── layout │ │ │ │ ├── senddebugreport_dialog.xml │ │ │ │ ├── unofficialbatteryapi_dialog.xml │ │ │ │ ├── activity_main.xml │ │ │ │ └── content_main.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── drawable │ │ │ │ └── ic_launcher_background.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── communitycode │ │ │ │ └── amps │ │ │ │ └── main │ │ │ │ ├── battery │ │ │ │ ├── BatteryMethodInterface.java │ │ │ │ ├── OfficialBatteryMethod.java │ │ │ │ ├── UnofficialBatteryMethod.java │ │ │ │ ├── reader │ │ │ │ │ ├── OneLineReader.java │ │ │ │ │ ├── SMemTextReader.java │ │ │ │ │ └── BatteryAttrTextReader.java │ │ │ │ └── UnofficialBatteryApi.java │ │ │ │ ├── settings │ │ │ │ ├── SettingsFragment.java │ │ │ │ ├── SettingsActivity.java │ │ │ │ ├── BatteryMethodPickler.java │ │ │ │ ├── UnofficialBatteryMethodAdapter.java │ │ │ │ └── UnofficialBatteryApiPreference.java │ │ │ │ ├── Utils.java │ │ │ │ ├── BatteryInfoInterface.java │ │ │ │ ├── BatteryInfoAlertDialog.java │ │ │ │ ├── BatteryPresenter.java │ │ │ │ ├── CurrentTracker.java │ │ │ │ └── MainActivity.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── communitycode │ │ │ └── amps │ │ │ └── main │ │ │ ├── UtilsUnitTest.java │ │ │ ├── CurrentTrackerUnitTest.java │ │ │ └── settings │ │ │ └── BatteryMethodPicklerTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── communitycode │ │ └── amps │ │ └── main │ │ ├── UtilsInstrumentedTest.java │ │ └── ActivityTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── meta ├── short_description.txt ├── feature-graphic.png └── full_description.txt ├── .gitignore ├── readme.md ├── gradle.properties ├── gradlew.bat ├── gradlew └── license.md /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /meta/short_description.txt: -------------------------------------------------------------------------------- 1 | Check how fast your battery is charging. No Ads, Open Source. 2 | -------------------------------------------------------------------------------- /meta/feature-graphic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/community-code/amps/HEAD/meta/feature-graphic.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/community-code/amps/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/community-code/amps/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/community-code/amps/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/community-code/amps/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/community-code/amps/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/community-code/amps/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/community-code/amps/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/community-code/amps/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/community-code/amps/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/community-code/amps/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/battery/BatteryMethodInterface.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main.battery; 2 | 3 | public interface BatteryMethodInterface { 4 | Integer read(); 5 | } 6 | -------------------------------------------------------------------------------- /app/src/main/res/xml/backup_descriptor.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | /.idea 7 | /app/release 8 | /gradle 9 | .DS_Store 10 | /build 11 | /captures 12 | .externalNativeBuild 13 | keystore.properties 14 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | @android:color/darker_gray 4 | @android:color/holo_blue_bright 5 | @android:color/holo_orange_light 6 | #20B2AA 7 | 8 | -------------------------------------------------------------------------------- /meta/full_description.txt: -------------------------------------------------------------------------------- 1 | Do you think your phone is charging slowly? This app lets you check if your charger/cable is working! 2 | 3 | Measure how quickly your battery is charging/discharging. Immediately. No waiting! 4 | 5 | Not every device is supported. Not mA accurate but as accurate as can be. This software is still in alpha. Android 4.2.2+ 6 | 7 | Please send any questions or comments so I can improve this app. 8 | -------------------------------------------------------------------------------- /app/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 | 5 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/settings/SettingsFragment.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main.settings; 2 | 3 | import android.os.Bundle; 4 | import android.preference.PreferenceFragment; 5 | 6 | import com.communitycode.amps.main.R; 7 | 8 | public class SettingsFragment extends PreferenceFragment { 9 | @Override 10 | public void onCreate(Bundle savedInstanceState) { 11 | super.onCreate(savedInstanceState); 12 | 13 | // Load the preferences from an XML resource 14 | addPreferencesFromResource(R.xml.settings); 15 | } 16 | } -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Amps 2 | 3 | Do you think your phone is charging slowly? This app lets you check if your charger/cable is working! 4 | 5 | Measure how quickly your battery is charging/discharging. Immediately. No waiting! 6 | 7 | Not every device is supported. Not mA accurate but as accurate as can be. This software is still in alpha. Android 4.2.2+ 8 | 9 | Please send any questions or comments so I can improve this app. 10 | 11 | # License 12 | 13 | gpl-3.0 14 | 15 | # Acknowledgements 16 | 17 | [Currentwidget](https://github.com/rmanor/currentwidget) did the hard work of supporting devices that do not support the official api. 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/settings/SettingsActivity.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main.settings; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | 6 | public class SettingsActivity extends AppCompatActivity { 7 | @Override 8 | protected void onCreate(Bundle savedInstanceState) { 9 | super.onCreate(savedInstanceState); 10 | 11 | 12 | // Display the fragment as the main content. 13 | getFragmentManager().beginTransaction() 14 | .replace(android.R.id.content, new SettingsFragment()) 15 | .commit(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/test/java/com/communitycode/amps/main/UtilsUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main; 2 | 3 | import org.junit.Test; 4 | 5 | import static com.communitycode.amps.main.Utils.convertCelsiusToFahrenheit; 6 | import static org.junit.Assert.*; 7 | 8 | /** 9 | * Example local unit test, which will execute on the development machine (host). 10 | * 11 | * @see Testing documentation 12 | */ 13 | public class UtilsUnitTest { 14 | @Test 15 | public void convertCelsiusToFahrenheit_sanity() throws Exception { 16 | assertEquals(32, convertCelsiusToFahrenheit(0), 0); 17 | assertEquals(212, convertCelsiusToFahrenheit(100), 0); 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/res/layout/senddebugreport_dialog.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 16 | 17 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/Utils.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main; 2 | 3 | import android.view.View; 4 | import android.view.ViewGroup; 5 | 6 | import java.util.ArrayList; 7 | 8 | public class Utils { 9 | public static ArrayList flattenViewGroup(ViewGroup root) { 10 | ArrayList views = new ArrayList<>(); 11 | if (root == null) { 12 | return views; 13 | } 14 | 15 | for (int i = 0; i < root.getChildCount(); i++) { 16 | final View child = root.getChildAt(i); 17 | views.add(child); 18 | if (child instanceof ViewGroup) { 19 | views.addAll(flattenViewGroup((ViewGroup) child)); 20 | } 21 | } 22 | return views; 23 | } 24 | 25 | public static double convertCelsiusToFahrenheit(double value) { 26 | return value*1.8 + 32; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/res/layout/unofficialbatteryapi_dialog.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 10 | 16 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/BatteryInfoInterface.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main; 2 | 3 | public interface BatteryInfoInterface { 4 | // value in milliamps 5 | void setMaxAmps(Integer value); 6 | 7 | // value in milliamps 8 | void setMinAmps(Integer value); 9 | 10 | // value in milliamps 11 | void setCurrentAmps(Integer value); 12 | 13 | // value in volts 14 | void setVoltage(Double value); 15 | 16 | // value in Celsius 17 | void setTemperature(Double value); 18 | 19 | // status one of BatteryManager.BATTERY_STATUS_* 20 | void setChargingStatus(int status); 21 | 22 | // plugged one of BatteryManager.BATTERY_PLUGGED_* 23 | void setPluggedInStatus(int plugged); 24 | 25 | // health one of BatteryManager.BATTERY_HEALTH_* 26 | void setBatteryHealth(int health); 27 | 28 | void setBatteryPercent(Double value); 29 | 30 | void setBatteryTechnology(String value); 31 | 32 | void showAmpInfoButton(boolean visible); 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/battery/OfficialBatteryMethod.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main.battery; 2 | 3 | import android.content.Context; 4 | import android.os.BatteryManager; 5 | import android.os.Build; 6 | 7 | public class OfficialBatteryMethod implements BatteryMethodInterface { 8 | private final transient Context mCtx; 9 | 10 | public OfficialBatteryMethod(Context context) { 11 | mCtx = context; 12 | } 13 | public Integer read() { 14 | if (Build.VERSION.SDK_INT < 21) { 15 | return null; 16 | } 17 | 18 | BatteryManager mBatteryManager = (BatteryManager) mCtx.getSystemService(Context.BATTERY_SERVICE); 19 | if (mBatteryManager == null) { 20 | return null; 21 | } 22 | 23 | int current = mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW); 24 | if (current != Integer.MIN_VALUE) { 25 | return current / 1000; 26 | } 27 | 28 | return null; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/res/xml/settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 11 | 12 | 17 | 18 | 20 | 21 | 22 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/BatteryInfoAlertDialog.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main; 2 | 3 | import android.app.Dialog; 4 | import android.content.DialogInterface; 5 | import android.content.Intent; 6 | import android.os.Bundle; 7 | import android.support.annotation.NonNull; 8 | import android.support.v4.app.DialogFragment; 9 | import android.support.v7.app.AlertDialog; 10 | 11 | import com.communitycode.amps.main.settings.SettingsActivity; 12 | 13 | public class BatteryInfoAlertDialog extends DialogFragment { 14 | @NonNull 15 | @Override 16 | public Dialog onCreateDialog(Bundle savedInstanceState) { 17 | // Use the Builder class for convenient dialog construction 18 | AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 19 | builder.setMessage(R.string.battery_info_alert_message) 20 | .setPositiveButton(android.R.string.ok, null) 21 | .setNegativeButton(R.string.go_to_battery_info_settings, new DialogInterface.OnClickListener(){ 22 | @Override 23 | public void onClick(DialogInterface dialog, int which) { 24 | Intent intent = new Intent(getContext(), SettingsActivity.class); 25 | startActivity(intent); 26 | } 27 | }); 28 | // Create the AlertDialog object and return it 29 | return builder.create(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 16 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/test/java/com/communitycode/amps/main/CurrentTrackerUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertEquals; 6 | import static org.mockito.Mockito.mock; 7 | import static org.mockito.Mockito.verify; 8 | 9 | public class CurrentTrackerUnitTest { 10 | @Test 11 | public void addHistory_overflow() throws Exception { 12 | BatteryInfoInterface mockBatteryInfo = mock(BatteryInfoInterface.class); 13 | CurrentTracker currentTracker = new CurrentTracker(null, mockBatteryInfo); 14 | 15 | for (int i = 0; i < CurrentTracker.MAX_HISTORY*2; i++) { 16 | currentTracker.addHistory(i); 17 | } 18 | 19 | assertEquals(CurrentTracker.MAX_HISTORY, currentTracker.currentHistory.size()); 20 | 21 | assertEquals( "Check first in first out", 22 | CurrentTracker.MAX_HISTORY, currentTracker.currentHistory.get(0).intValue()); 23 | } 24 | 25 | @Test 26 | public void updateAmpStatistics_emptyHistory() throws Exception { 27 | BatteryInfoInterface mockBatteryInfo = mock(BatteryInfoInterface.class); 28 | 29 | CurrentTracker currentTracker = new CurrentTracker(null, mockBatteryInfo); 30 | 31 | currentTracker.updateAmpStatistics(); 32 | 33 | verify(mockBatteryInfo).setMinAmps(null); 34 | verify(mockBatteryInfo).setMaxAmps(null); 35 | verify(mockBatteryInfo).setCurrentAmps(null); 36 | } 37 | 38 | @Test 39 | public void updateAmpStatistics_hasHistory() throws Exception { 40 | BatteryInfoInterface mockBatteryInfo = mock(BatteryInfoInterface.class); 41 | 42 | CurrentTracker currentTracker = new CurrentTracker(null, mockBatteryInfo); 43 | currentTracker.addHistory(10); 44 | currentTracker.addHistory(-10); 45 | currentTracker.addHistory(5); 46 | currentTracker.addHistory(10); 47 | currentTracker.addHistory(-3); 48 | 49 | currentTracker.updateAmpStatistics(); 50 | 51 | verify(mockBatteryInfo).setMinAmps(-10); 52 | verify(mockBatteryInfo).setMaxAmps(10); 53 | verify(mockBatteryInfo).setCurrentAmps(-3); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | // Create a variable called keystorePropertiesFile, and initialize it to your 4 | // keystore.properties file, in the rootProject folder. 5 | def keystorePropertiesFile = rootProject.file("keystore.properties") 6 | 7 | // Initialize a new Properties() object called keystoreProperties. 8 | def keystoreProperties = new Properties() 9 | 10 | // Load your keystore.properties file into the keystoreProperties object. 11 | keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) 12 | 13 | android { 14 | signingConfigs { 15 | config { 16 | keyAlias keystoreProperties['keyAlias'] 17 | keyPassword keystoreProperties['keyPassword'] 18 | storeFile file(keystoreProperties['storeFile']) 19 | storePassword keystoreProperties['storePassword'] 20 | } 21 | } 22 | compileSdkVersion 26 23 | defaultConfig { 24 | applicationId "com.communitycode.amps.main" 25 | minSdkVersion 17 26 | targetSdkVersion 26 27 | versionCode 6 28 | versionName "6.0" 29 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 30 | } 31 | buildTypes { 32 | release { 33 | minifyEnabled true 34 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 35 | pseudoLocalesEnabled true 36 | signingConfig signingConfigs.config 37 | } 38 | debug { 39 | pseudoLocalesEnabled true 40 | } 41 | } 42 | productFlavors { 43 | } 44 | } 45 | 46 | dependencies { 47 | implementation fileTree(include: ['*.jar'], dir: 'libs') 48 | implementation 'com.android.support:appcompat-v7:26.1.0' 49 | implementation 'com.android.support.constraint:constraint-layout:1.0.2' 50 | implementation 'com.android.support:design:26.1.0' 51 | testImplementation 'junit:junit:4.12' 52 | testImplementation 'org.mockito:mockito-core:1.10.19' 53 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 54 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 55 | implementation 'com.google.code.gson:gson:2.8.2' 56 | } 57 | 58 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/communitycode/amps/main/UtilsInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.LinearLayout; 9 | 10 | import org.junit.Test; 11 | import org.junit.runner.RunWith; 12 | 13 | import static com.communitycode.amps.main.Utils.flattenViewGroup; 14 | import static org.junit.Assert.*; 15 | 16 | /** 17 | * Instrumented test, which will execute on an Android device. 18 | * 19 | * @see Testing documentation 20 | */ 21 | @RunWith(AndroidJUnit4.class) 22 | public class UtilsInstrumentedTest { 23 | @Test 24 | public void flattenViewGroup_null() throws Exception { 25 | View[] expected = {}; 26 | assertArrayEquals(expected, flattenViewGroup(null).toArray()); 27 | } 28 | 29 | 30 | @Test 31 | public void flattenViewGroup_singleElement() throws Exception { 32 | Context appContext = InstrumentationRegistry.getTargetContext(); 33 | ViewGroup viewGroup = new LinearLayout(appContext); 34 | View A = new View(appContext); 35 | viewGroup.addView(A); 36 | View[] expected = { 37 | A 38 | }; 39 | assertArrayEquals(expected, flattenViewGroup(viewGroup).toArray()); 40 | } 41 | 42 | @Test 43 | public void flattenViewGroup_multipleElements() throws Exception { 44 | Context appContext = InstrumentationRegistry.getTargetContext(); 45 | ViewGroup viewGroup = new LinearLayout(appContext); 46 | View A = new View(appContext); 47 | View B = new View(appContext); 48 | viewGroup.addView(A); 49 | viewGroup.addView(B); 50 | View[] expected = { 51 | A, B 52 | }; 53 | assertArrayEquals(expected, flattenViewGroup(viewGroup).toArray()); 54 | } 55 | 56 | @Test 57 | public void flattenViewGroup_nestedElements() throws Exception { 58 | Context appContext = InstrumentationRegistry.getTargetContext(); 59 | ViewGroup viewGroup = new LinearLayout(appContext); 60 | View A = new View(appContext); 61 | View B = new View(appContext); 62 | ViewGroup C = new LinearLayout(appContext); 63 | viewGroup.addView(A); 64 | viewGroup.addView(C); 65 | C.addView(B); 66 | View[] expected = { 67 | A, C, B 68 | }; 69 | assertArrayEquals(expected, flattenViewGroup(viewGroup).toArray()); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/battery/UnofficialBatteryMethod.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main.battery; 2 | 3 | 4 | import com.communitycode.amps.main.battery.reader.BatteryAttrTextReader; 5 | import com.communitycode.amps.main.battery.reader.OneLineReader; 6 | import com.communitycode.amps.main.battery.reader.SMemTextReader; 7 | 8 | import java.io.File; 9 | 10 | public class UnofficialBatteryMethod implements BatteryMethodInterface { 11 | public String dischargeField; 12 | public String chargeField; 13 | public String filePath; 14 | public float scale; 15 | public int reader; 16 | public transient String[] modelFilter; 17 | 18 | public UnofficialBatteryMethod(int reader, String filePath, float scale, String dischargeField, String chargeField, String[] modelFilter) { 19 | this.filePath = filePath; 20 | this.scale = scale; 21 | this.reader = reader; 22 | this.dischargeField = dischargeField; 23 | this.chargeField = chargeField; 24 | this.modelFilter = modelFilter; 25 | } 26 | 27 | public boolean checkModelFilter(String model) { 28 | if (modelFilter.length == 0) { 29 | return true; 30 | } 31 | 32 | for (String val : modelFilter) { 33 | if (model.contains(val)) { 34 | return true; 35 | } 36 | } 37 | return false; 38 | } 39 | 40 | public boolean isApplicable(String model) { 41 | if (checkModelFilter(model)) { 42 | File f = new File(filePath); 43 | return f.exists(); 44 | } 45 | return false; 46 | } 47 | 48 | public Integer read() { 49 | File f = new File(filePath); 50 | Integer val = null; 51 | 52 | switch (reader) { 53 | case 1: 54 | val = OneLineReader.getValue(f); 55 | break; 56 | case 2: 57 | val = BatteryAttrTextReader.getValue(f, this.dischargeField, this.chargeField); 58 | break; 59 | case 3: 60 | val = SMemTextReader.getValue(); 61 | break; 62 | } 63 | 64 | if (val == null) { 65 | return null; 66 | } 67 | else { 68 | return Math.round(val * scale); 69 | } 70 | } 71 | 72 | public boolean equalsIgnoreTransient(UnofficialBatteryMethod b) { 73 | return filePath.equals(b.filePath) 74 | && reader == b.reader 75 | && scale == b.scale 76 | && chargeField.equals(b.chargeField) 77 | && dischargeField.equals(b.dischargeField); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/battery/reader/OneLineReader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Ran Manor 3 | * 4 | * This file is part of CurrentWidget. 5 | * 6 | * CurrentWidget is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * CurrentWidget is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with CurrentWidget. If not, see . 18 | * 19 | * Modified 23 March 2018 20 | */ 21 | 22 | package com.communitycode.amps.main.battery.reader; 23 | 24 | 25 | import android.util.Log; 26 | 27 | import java.io.BufferedReader; 28 | import java.io.File; 29 | import java.io.FileInputStream; 30 | import java.io.InputStreamReader; 31 | 32 | 33 | public class OneLineReader { 34 | 35 | 36 | public static Integer getValue(File _f) { 37 | 38 | String text = null; 39 | 40 | FileInputStream fs = null; 41 | InputStreamReader sr = null; 42 | BufferedReader br = null; 43 | try { 44 | fs = new FileInputStream(_f); 45 | sr = new InputStreamReader(fs); 46 | br = new BufferedReader(sr); 47 | 48 | text = br.readLine(); 49 | 50 | } catch (Exception ex) { 51 | // Expected to fail frequently due to permissions 52 | Log.d("Amps", ex.getMessage(), ex); 53 | } 54 | finally 55 | { 56 | try { 57 | if (fs != null) { 58 | fs.close(); 59 | } 60 | if (sr != null) { 61 | sr.close(); 62 | } 63 | if (br != null) { 64 | br.close(); 65 | } 66 | } 67 | catch (Exception ex) 68 | { 69 | Log.e("Amps", ex.getMessage(), ex); 70 | } 71 | } 72 | 73 | Integer value = null; 74 | 75 | try { 76 | if (text != null) { 77 | value = Integer.parseInt(text); 78 | } 79 | } catch (NumberFormatException nfe) { 80 | // Expected to fail frequently due to unknown format of text 81 | Log.d("Amps", nfe.getMessage(), nfe); 82 | } 83 | 84 | return value; 85 | } 86 | } -------------------------------------------------------------------------------- /app/src/test/java/com/communitycode/amps/main/settings/BatteryMethodPicklerTest.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main.settings; 2 | 3 | import com.communitycode.amps.main.BatteryInfoInterface; 4 | import com.communitycode.amps.main.CurrentTracker; 5 | import com.communitycode.amps.main.battery.BatteryMethodInterface; 6 | import com.communitycode.amps.main.battery.OfficialBatteryMethod; 7 | import com.communitycode.amps.main.battery.UnofficialBatteryMethod; 8 | 9 | import org.junit.Test; 10 | 11 | import static org.junit.Assert.assertEquals; 12 | import static org.junit.Assert.assertTrue; 13 | import static org.mockito.Mockito.mock; 14 | 15 | public class BatteryMethodPicklerTest { 16 | @Test 17 | public void official() throws Exception { 18 | OfficialBatteryMethod officialBatteryMethod = new OfficialBatteryMethod(null); 19 | String json = BatteryMethodPickler.toJson(officialBatteryMethod); 20 | BatteryMethodInterface method = BatteryMethodPickler.fromJson(json, null); 21 | assertTrue(OfficialBatteryMethod.class.isInstance(method)); 22 | } 23 | 24 | @Test 25 | public void unofficial_null() throws Exception { 26 | UnofficialBatteryMethod unofficialBatteryMethod = new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/current_max", 1.0F, null, null, new String[]{}); 27 | String json = BatteryMethodPickler.toJson(unofficialBatteryMethod); 28 | UnofficialBatteryMethod method = (UnofficialBatteryMethod) BatteryMethodPickler.fromJson(json, null); 29 | assertEquals(method.chargeField, unofficialBatteryMethod.chargeField); 30 | assertEquals(method.dischargeField, unofficialBatteryMethod.dischargeField); 31 | assertEquals(method.filePath, unofficialBatteryMethod.filePath); 32 | assertEquals(method.reader, unofficialBatteryMethod.reader); 33 | assertEquals(method.scale, unofficialBatteryMethod.scale, 0.01); 34 | } 35 | 36 | @Test 37 | public void unofficial_full() throws Exception { 38 | UnofficialBatteryMethod unofficialBatteryMethod = new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/current_max", 1.0F, "asdf", "fdsa", new String[]{}); 39 | String json = BatteryMethodPickler.toJson(unofficialBatteryMethod); 40 | UnofficialBatteryMethod method = (UnofficialBatteryMethod) BatteryMethodPickler.fromJson(json, null); 41 | assertEquals(method.chargeField, unofficialBatteryMethod.chargeField); 42 | assertEquals(method.dischargeField, unofficialBatteryMethod.dischargeField); 43 | assertEquals(method.filePath, unofficialBatteryMethod.filePath); 44 | assertEquals(method.reader, unofficialBatteryMethod.reader); 45 | assertEquals(method.scale, unofficialBatteryMethod.scale, 0.01); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Amps 3 | Cold 4 | Dead 5 | Good 6 | Over voltage 7 | Overheat 8 | Unspecified failure 9 | Charging 10 | Discharging 11 | Full 12 | On battery 13 | Wireless 14 | USB 15 | AC 16 | --- 17 | min: 18 | Plugged 19 | Level 20 | Health 21 | Technology 22 | Temperature 23 | Voltage 24 | Android version 25 | Model 26 | Build ID 27 | max: 28 | Status 29 | accent 30 | %d mA 31 | %.1f °C 32 | %.1f °F 33 | %.3f V 34 | \ 35 | 36 | Use Celsius 37 | Units 38 | Settings 39 | Debug 40 | About Amps 41 | License 42 | Amperage Measurement 43 | Value %d 44 | Select method of getting the amperage 45 | \u24D8 46 | Default 47 | There are multiple ways to determine battery current. See the amperage measurement setting for details. 48 | Settings 49 | Send Error Report 50 | Send an error report to help improve the app 51 | 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/BatteryPresenter.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main; 2 | 3 | import android.content.BroadcastReceiver; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.content.IntentFilter; 7 | import android.os.BatteryManager; 8 | import android.os.Build; 9 | import android.util.Log; 10 | 11 | public class BatteryPresenter { 12 | private final BatteryInfoInterface mBatteryInfoInterface; 13 | private final Context mCtx; 14 | private final CurrentTracker mCurrentTracker; 15 | 16 | private final BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() { 17 | @Override 18 | public void onReceive(Context context, Intent intent) { 19 | try { 20 | // Avoid crashing the app 21 | updateBatteryData(intent); 22 | } catch(Exception e) { 23 | Log.e("error", e.getMessage()); 24 | } 25 | } 26 | }; 27 | 28 | private void updateBatteryData(Intent intent) { 29 | int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0); 30 | mBatteryInfoInterface.setBatteryHealth(health); 31 | 32 | int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); 33 | int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); 34 | boolean isError = level == -1 || scale == -1 || scale == 0; 35 | mBatteryInfoInterface.setBatteryPercent(isError ? null : level /(double) scale); 36 | 37 | int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0); 38 | mBatteryInfoInterface.setPluggedInStatus(plugged); 39 | 40 | int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); 41 | mBatteryInfoInterface.setChargingStatus(status); 42 | 43 | String technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY); 44 | isError = technology == null || "".equals(technology); 45 | mBatteryInfoInterface.setBatteryTechnology(isError ? null : technology); 46 | 47 | double temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0)/10f; 48 | mBatteryInfoInterface.setTemperature(temperature > 0 ? temperature : null); 49 | 50 | double voltage = ((double) intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0))/1000; 51 | mBatteryInfoInterface.setVoltage(voltage > 0 ? voltage : null); 52 | } 53 | 54 | public BatteryPresenter(Context ctx, BatteryInfoInterface batteryInfoInterface) { 55 | mBatteryInfoInterface = batteryInfoInterface; 56 | mCtx = ctx; 57 | mCurrentTracker = new CurrentTracker(ctx, batteryInfoInterface); 58 | } 59 | 60 | public void resetCurrentHistory() { 61 | mCurrentTracker.resetHistory(); 62 | } 63 | 64 | public void start() { 65 | IntentFilter intentFilter = new IntentFilter(); 66 | intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); 67 | mCtx.registerReceiver(batteryInfoReceiver, intentFilter); 68 | 69 | mCurrentTracker.start(); 70 | } 71 | 72 | public void stop() { 73 | mCtx.unregisterReceiver(batteryInfoReceiver); 74 | 75 | mCurrentTracker.stop(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 15 | 16 | 25 | 26 | 29 | 30 | 36 | 37 | 47 | 48 | 53 | 54 | 59 | 60 | 67 | 68 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/battery/reader/SMemTextReader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2011 Ran Manor 3 | * 4 | * This file is part of CurrentWidget. 5 | * 6 | * CurrentWidget is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * CurrentWidget is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with CurrentWidget. If not, see . 18 | * 19 | * Modified 23 March 2018 20 | */ 21 | 22 | package com.communitycode.amps.main.battery.reader; 23 | 24 | import java.io.BufferedReader; 25 | import java.io.FileReader; 26 | 27 | import android.util.Log; 28 | 29 | public class SMemTextReader { 30 | 31 | public static Integer getValue() { 32 | 33 | boolean success = false; 34 | String text = null; 35 | BufferedReader br = null; 36 | FileReader fr = null; 37 | 38 | try { 39 | 40 | // @@@ debug StringReader fr = new StringReader("batt_id: 1\r\nbatt_vol: 3840\r\nbatt_vol_last: 0\r\nbatt_temp: 1072\r\nbatt_current: 1\r\nbatt_current_last: 0\r\nbatt_discharge_current: 112\r\nVREF_2: 0\r\nVREF: 1243\r\nADC4096_VREF: 4073\r\nRtemp: 70\r\nTemp: 324\r\nTemp_last: 0\r\npd_M: 20\r\nMBAT_pd: 3860\r\nI_MBAT: -114\r\npd_temp: 0\r\npercent_last: 57\r\npercent_update: 58\r\ndis_percent: 64\r\nvbus: 0\r\nusbid: 1\r\ncharging_source: 0\r\nMBAT_IN: 1\r\nfull_bat: 1300000\r\neval_current: 115\r\neval_current_last: 0\r\ncharging_enabled: 0\r\ntimeout: 30\r\nfullcharge: 0\r\nlevel: 58\r\ndelta: 1\r\nchg_time: 0\r\nlevel_change: 0\r\nsleep_timer_count: 11\r\nOT_led_on: 0\r\noverloading_charge: 0\r\na2m_cable_type: 0\r\nover_vchg: 0\r\n"); 41 | fr = new FileReader("/sys/class/power_supply/battery/smem_text"); 42 | br = new BufferedReader(fr); 43 | 44 | String line = br.readLine(); 45 | 46 | while (line != null) { 47 | if (line.contains("I_MBAT")) { 48 | text = line.substring(line.indexOf("I_MBAT: ") + 8); 49 | success = true; 50 | break; 51 | } 52 | line = br.readLine(); 53 | } 54 | } catch (Exception ex) { 55 | Log.e("Amps", ex.getMessage(), ex); 56 | } 57 | finally 58 | { 59 | try { 60 | if (fr != null) { 61 | fr.close(); 62 | } 63 | if (br != null) { 64 | br.close(); 65 | } 66 | } 67 | catch (Exception ex) 68 | { 69 | Log.e("Amps", ex.getMessage(), ex); 70 | } 71 | } 72 | 73 | 74 | Integer value = null; 75 | 76 | if (success) { 77 | 78 | try { 79 | value = Integer.parseInt(text); 80 | } catch (NumberFormatException nfe) { 81 | Log.e("Amps", nfe.getMessage(), nfe); 82 | value = null; 83 | } 84 | } 85 | 86 | return value; 87 | } 88 | 89 | } -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/settings/BatteryMethodPickler.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main.settings; 2 | 3 | import android.content.Context; 4 | import android.util.Log; 5 | 6 | import com.communitycode.amps.main.battery.BatteryMethodInterface; 7 | import com.communitycode.amps.main.battery.OfficialBatteryMethod; 8 | import com.communitycode.amps.main.battery.UnofficialBatteryMethod; 9 | import com.google.gson.Gson; 10 | import com.google.gson.GsonBuilder; 11 | import com.google.gson.InstanceCreator; 12 | import com.google.gson.JsonElement; 13 | import com.google.gson.JsonObject; 14 | import com.google.gson.JsonParser; 15 | 16 | import java.lang.reflect.Type; 17 | 18 | public class BatteryMethodPickler { 19 | private static Class[] x = new Class[]{OfficialBatteryMethod.class, UnofficialBatteryMethod.class}; 20 | public static String DISCHARGEFIELD = "DISCHARGEFIELD"; 21 | public static String CHARGEFIELD = "CHARGEFIELD"; 22 | public static String FILEPATH = "FILEPATH"; 23 | public static String SCALE = "SCALE"; 24 | public static String READER = "READER"; 25 | public static String TYPE = "TYPE"; 26 | public static String OFFICIALBATTERYMETHOD = "OFFICIALBATTERYMETHOD"; 27 | public static String UNOFFICIALBATTERYMETHOD = "UNOFFICIALBATTERYMETHOD"; 28 | 29 | 30 | public static BatteryMethodInterface fromJson(String json, Context mCtx) { 31 | if (json == null) { 32 | return null; 33 | } 34 | 35 | try { 36 | Gson gson = new Gson(); 37 | JsonObject jsonObject = gson.fromJson(json, JsonObject.class); 38 | String type = jsonObject.get(TYPE).getAsString(); 39 | if (type.equals(OFFICIALBATTERYMETHOD)) { 40 | return new OfficialBatteryMethod(mCtx); 41 | } 42 | else if (type.equals(UNOFFICIALBATTERYMETHOD)) { 43 | return new UnofficialBatteryMethod( 44 | jsonObject.get(READER).getAsInt(), 45 | jsonObject.get(FILEPATH).getAsString(), 46 | jsonObject.get(SCALE).getAsFloat(), 47 | jsonObject.has(DISCHARGEFIELD) ? jsonObject.get(DISCHARGEFIELD).getAsString() : null, 48 | jsonObject.has(CHARGEFIELD) ? jsonObject.get(CHARGEFIELD).getAsString() : null, 49 | new String[] {}); 50 | } 51 | else { 52 | Log.d("Amps", "Unknown method. Json="+json); 53 | } 54 | } 55 | catch (Exception e) { 56 | Log.d("Amps", "Failed to parse preference. Json=" + json + " error=" + e.getMessage()); 57 | } 58 | return null; 59 | } 60 | 61 | 62 | public static String toJson(BatteryMethodInterface obj) { 63 | if (OfficialBatteryMethod.class.isInstance(obj)) { 64 | JsonObject jsonObject = new JsonObject(); 65 | jsonObject.addProperty(TYPE, OFFICIALBATTERYMETHOD); 66 | Gson gson = new Gson(); 67 | return gson.toJson(jsonObject); 68 | } 69 | else if (UnofficialBatteryMethod.class.isInstance(obj)) { 70 | UnofficialBatteryMethod method = (UnofficialBatteryMethod) obj; 71 | JsonObject jsonObject = new JsonObject(); 72 | jsonObject.addProperty(TYPE, UNOFFICIALBATTERYMETHOD); 73 | jsonObject.addProperty(DISCHARGEFIELD, method.dischargeField); 74 | jsonObject.addProperty(CHARGEFIELD, method.chargeField); 75 | jsonObject.addProperty(FILEPATH, method.filePath); 76 | jsonObject.addProperty(READER, method.reader); 77 | jsonObject.addProperty(SCALE, method.scale); 78 | Gson gson = new Gson(); 79 | return gson.toJson(jsonObject); 80 | } 81 | 82 | return null; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/battery/reader/BatteryAttrTextReader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2013 Ran Manor 3 | * 4 | * This file is part of CurrentWidget. 5 | * 6 | * CurrentWidget is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * CurrentWidget is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with CurrentWidget. If not, see . 18 | * 19 | * Modified 23 March 2018 20 | */ 21 | 22 | 23 | package com.communitycode.amps.main.battery.reader; 24 | 25 | 26 | import java.io.BufferedReader; 27 | import java.io.File; 28 | import java.io.FileReader; 29 | 30 | import android.util.Log; 31 | 32 | public class BatteryAttrTextReader { 33 | 34 | public static Integer getValue(File f, String dischargeField, String chargeField) { 35 | 36 | String text; 37 | Integer value = null; 38 | FileReader fr = null; 39 | BufferedReader br = null; 40 | 41 | try { 42 | 43 | // @@@ debug 44 | //StringReader fr = new StringReader("vref: 1248\r\nbatt_id: 3\r\nbatt_vol: 4068\r\nbatt_current: 0\r\nbatt_discharge_current: 123\r\nbatt_temperature: 329\r\nbatt_temp_protection:normal\r\nPd_M:0\r\nI_MBAT:-313\r\npercent_last(RP): 94\r\npercent_update: 71\r\nlevel: 71\r\nfirst_level: 100\r\nfull_level:100\r\ncapacity:1580\r\ncharging_source: USB\r\ncharging_enabled: Slow\r\n"); 45 | fr = new FileReader(f); 46 | br = new BufferedReader(fr); 47 | 48 | String line = br.readLine(); 49 | 50 | final String chargeFieldHead = chargeField + ": "; 51 | final String dischargeFieldHead = dischargeField + ": "; 52 | 53 | 54 | while (line != null) 55 | { 56 | if (line.contains(chargeField)) 57 | { 58 | text = line.substring(line.indexOf(chargeFieldHead) + chargeFieldHead.length()); 59 | try { 60 | value = Integer.parseInt(text); 61 | if (value != 0) 62 | break; 63 | } 64 | catch (NumberFormatException nfe) { 65 | Log.e("Amps", nfe.getMessage(), nfe); 66 | } 67 | } 68 | 69 | // "batt_discharge_current:" 70 | if (line.contains(dischargeField)) 71 | { 72 | text = line.substring(line.indexOf(dischargeFieldHead) + dischargeFieldHead.length()); 73 | try { 74 | value = (-1)*Math.abs(Integer.parseInt(text)); 75 | } 76 | catch (NumberFormatException nfe) { 77 | Log.e("Amps", nfe.getMessage(), nfe); 78 | } 79 | break; 80 | } 81 | 82 | line = br.readLine(); 83 | } 84 | } 85 | catch (Exception ex) { 86 | Log.e("Amps", ex.getMessage(), ex); 87 | } 88 | finally 89 | { 90 | try { 91 | if (fr != null) { 92 | fr.close(); 93 | } 94 | if (br != null) { 95 | br.close(); 96 | } 97 | } 98 | catch (Exception ex) 99 | { 100 | Log.e("Amps", ex.getMessage(), ex); 101 | } 102 | } 103 | 104 | return value; 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/CurrentTracker.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main; 2 | 3 | 4 | import android.content.Context; 5 | import android.content.SharedPreferences; 6 | import android.os.Handler; 7 | import android.preference.PreferenceManager; 8 | 9 | import com.communitycode.amps.main.battery.BatteryMethodInterface; 10 | import com.communitycode.amps.main.battery.OfficialBatteryMethod; 11 | import com.communitycode.amps.main.battery.UnofficialBatteryApi; 12 | import com.communitycode.amps.main.settings.BatteryMethodPickler; 13 | 14 | import java.util.ArrayList; 15 | 16 | public class CurrentTracker { 17 | protected static final int MAX_HISTORY = 1000; 18 | private static final int UPDATE_DELAY = 500; 19 | 20 | // current in milliamps 21 | protected ArrayList currentHistory = new ArrayList<>(); 22 | 23 | private Runnable sendData; 24 | final private Handler handler = new Handler(); 25 | final private Context mCtx; 26 | final private BatteryInfoInterface mBatteryInfoInterface; 27 | 28 | 29 | public CurrentTracker (Context ctx, BatteryInfoInterface batteryInfoInterface) { 30 | mCtx = ctx; 31 | mBatteryInfoInterface = batteryInfoInterface; 32 | sendData = new Runnable(){ 33 | public void run(){ 34 | try { 35 | updateAmps(); 36 | 37 | 38 | handler.postDelayed(this, UPDATE_DELAY); 39 | } 40 | catch (Exception e) { 41 | e.printStackTrace(); 42 | } 43 | } 44 | }; 45 | } 46 | 47 | public void start() { 48 | handler.post(sendData); 49 | } 50 | 51 | public void stop() { 52 | handler.removeCallbacks(sendData); 53 | } 54 | 55 | private void updateAmps() { 56 | Integer current = null; 57 | boolean showAmpInfo = true; 58 | 59 | // Get current by preference 60 | SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(mCtx); 61 | String json = sharedPref.getString("unofficial_measurement", null); 62 | BatteryMethodInterface method = BatteryMethodPickler.fromJson(json, mCtx); 63 | if (method != null) { 64 | current = method.read(); 65 | } 66 | else { 67 | // Get current by best guess 68 | current = new OfficialBatteryMethod(mCtx).read(); 69 | 70 | if (current == null || current == 0) { 71 | current = UnofficialBatteryApi.getCurrent(); 72 | } 73 | else { 74 | // Official method worked and preference is not set. No need to clutter the UI. 75 | showAmpInfo = false; 76 | } 77 | } 78 | 79 | mBatteryInfoInterface.showAmpInfoButton(showAmpInfo); 80 | 81 | if (current != null) { 82 | addHistory(current); 83 | updateAmpStatistics(); 84 | } 85 | } 86 | 87 | protected void addHistory(int value) { 88 | currentHistory.add(value); 89 | 90 | while (currentHistory.size() > MAX_HISTORY) { 91 | currentHistory.remove(0); 92 | } 93 | } 94 | 95 | public void resetHistory() { 96 | currentHistory.clear(); 97 | updateAmpStatistics(); 98 | } 99 | 100 | protected void updateAmpStatistics() { 101 | if (currentHistory.size() > 0) { 102 | int max = currentHistory.get(0); 103 | int min = currentHistory.get(0); 104 | for (int i = 0; i < currentHistory.size(); i++) { 105 | int val = currentHistory.get(i); 106 | max = val > max ? val : max; 107 | min = val < min ? val : min; 108 | } 109 | int last = currentHistory.get(currentHistory.size() - 1); 110 | mBatteryInfoInterface.setMaxAmps(max); 111 | mBatteryInfoInterface.setMinAmps(min); 112 | mBatteryInfoInterface.setCurrentAmps(last); 113 | } 114 | else { 115 | mBatteryInfoInterface.setMaxAmps(null); 116 | mBatteryInfoInterface.setMinAmps(null); 117 | mBatteryInfoInterface.setCurrentAmps(null); 118 | } 119 | } 120 | 121 | } 122 | -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/settings/UnofficialBatteryMethodAdapter.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main.settings; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.CheckBox; 9 | import android.widget.TextView; 10 | 11 | import com.communitycode.amps.main.R; 12 | 13 | import java.util.ArrayList; 14 | 15 | public class UnofficialBatteryMethodAdapter extends RecyclerView.Adapter { 16 | private int mCheckedPosition; 17 | private ArrayList mDataset; 18 | private OnClickHandler mOnClickListener; 19 | 20 | // Provide a reference to the views for each data item 21 | // Complex data items may need more than one view per item, and 22 | // you provide access to all the views for a data item in a view holder 23 | public static class ViewHolder extends RecyclerView.ViewHolder { 24 | private final CheckBox mCheckBox; 25 | // each data item is just a string in this case 26 | public View mView; 27 | public TextView mFilePath; 28 | public TextView mCurrentValue; 29 | 30 | public ViewHolder(View v) { 31 | super(v); 32 | mFilePath = v.findViewById(R.id.filePath); 33 | mCheckBox = v.findViewById(R.id.checkbox); 34 | mCurrentValue = v.findViewById(R.id.currentValue); 35 | mView = v; 36 | } 37 | } 38 | 39 | // Provide a suitable constructor (depends on the kind of dataset) 40 | public UnofficialBatteryMethodAdapter(ArrayList myDataset, int checkedPosition) { 41 | mDataset = myDataset; 42 | mCheckedPosition = checkedPosition; 43 | } 44 | 45 | // Create new views (invoked by the layout manager) 46 | @Override 47 | public UnofficialBatteryMethodAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, 48 | int viewType) { 49 | // create a new view 50 | Context context = parent.getContext(); 51 | LayoutInflater li = LayoutInflater.from(context); 52 | View view = li.inflate(R.layout.unofficialbatteryapi_dialog, parent, false); 53 | return new ViewHolder(view); 54 | } 55 | 56 | public void setOnClickListener(OnClickHandler onClickListener) { 57 | mOnClickListener = onClickListener; 58 | } 59 | 60 | // Replace the contents of a view (invoked by the layout manager) 61 | @Override 62 | public void onBindViewHolder(final ViewHolder holder, int position) { 63 | // - get element from your dataset at this position 64 | // - replace the contents of the view with that element 65 | 66 | Context context = holder.mView.getContext(); 67 | MethodInfo method = mDataset.get(position); 68 | 69 | View.OnClickListener onClickListener = new View.OnClickListener() { 70 | public void onClick(View v) { 71 | if (mOnClickListener != null) { 72 | mOnClickListener.onClick(holder.getAdapterPosition()); 73 | } 74 | } 75 | }; 76 | 77 | holder.mView.setOnClickListener(onClickListener); 78 | 79 | holder.mFilePath.setText(method.name); 80 | 81 | 82 | Integer val = method.value; 83 | if (val == null) { 84 | val = 0; 85 | } 86 | 87 | holder.mCurrentValue.setText(context.getString(R.string.value, val)); 88 | 89 | holder.mCheckBox.setChecked(mCheckedPosition == position); 90 | holder.mCheckBox.setOnClickListener(onClickListener); 91 | } 92 | 93 | public void setCheckedPosition(int position) { 94 | this.notifyItemChanged(mCheckedPosition); 95 | mCheckedPosition = position; 96 | this.notifyItemChanged(position); 97 | } 98 | 99 | // Return the size of your dataset (invoked by the layout manager) 100 | @Override 101 | public int getItemCount() { 102 | return mDataset.size(); 103 | } 104 | 105 | public interface OnClickHandler { 106 | void onClick(int position); 107 | } 108 | 109 | public static class MethodInfo { 110 | public String name; 111 | public Integer value; 112 | 113 | MethodInfo(String name, Integer value) { 114 | this.name = name; 115 | this.value = value; 116 | } 117 | } 118 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/communitycode/amps/main/ActivityTest.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main; 2 | 3 | // Disable test as intermediate progress bar prevents espresso's getActivity from working. 4 | // 5 | //import android.support.test.espresso.ViewInteraction; 6 | //import android.support.test.rule.ActivityTestRule; 7 | //import android.support.test.runner.AndroidJUnit4; 8 | //import android.test.suitebuilder.annotation.LargeTest; 9 | //import android.view.View; 10 | //import android.view.ViewGroup; 11 | //import android.view.ViewParent; 12 | // 13 | //import org.hamcrest.Description; 14 | //import org.hamcrest.Matcher; 15 | //import org.hamcrest.TypeSafeMatcher; 16 | //import org.junit.Rule; 17 | //import org.junit.Test; 18 | //import org.junit.runner.RunWith; 19 | // 20 | //import static android.support.test.InstrumentationRegistry.getInstrumentation; 21 | //import static android.support.test.espresso.Espresso.onView; 22 | //import static android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu; 23 | //import static android.support.test.espresso.action.ViewActions.click; 24 | //import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 25 | //import static android.support.test.espresso.matcher.ViewMatchers.withClassName; 26 | //import static android.support.test.espresso.matcher.ViewMatchers.withId; 27 | //import static android.support.test.espresso.matcher.ViewMatchers.withText; 28 | //import static org.hamcrest.Matchers.allOf; 29 | //import static org.hamcrest.Matchers.is; 30 | // 31 | // 32 | //@LargeTest 33 | //@RunWith(AndroidJUnit4.class) 34 | //public class ActivityTest { 35 | // @Rule 36 | // public ActivityTestRule mActivityTestRule = new ActivityTestRule<>(MainActivity.class); 37 | // 38 | // 39 | // @Test 40 | // public void activityTest() { 41 | // // Added a sleep statement to match the app's execution delay. 42 | // // The recommended way to handle such scenarios is to use Espresso idling resources: 43 | // // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html 44 | // try { 45 | // Thread.sleep(500); 46 | // } catch (InterruptedException e) { 47 | // e.printStackTrace(); 48 | // } 49 | // 50 | // ViewInteraction mainTextView = onView( 51 | // allOf(withId(R.id.title), withText("Amps"), 52 | // isDisplayed())); 53 | // 54 | // assert(mainTextView != null); 55 | // 56 | // openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext()); 57 | // 58 | // // Added a sleep statement to match the app's execution delay. 59 | // // The recommended way to handle such scenarios is to use Espresso idling resources: 60 | // // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html 61 | // try { 62 | // Thread.sleep(500); 63 | // } catch (InterruptedException e) { 64 | // e.printStackTrace(); 65 | // } 66 | // 67 | // ViewInteraction appCompatTextView = onView( 68 | // allOf(withId(R.id.title), withText("Settings"), 69 | // childAtPosition( 70 | // childAtPosition( 71 | // withClassName(is("android.support.v7.view.menu.ListMenuItemView")), 72 | // 0), 73 | // 0), 74 | // isDisplayed())); 75 | // appCompatTextView.perform(click()); 76 | // 77 | // try { 78 | // Thread.sleep(500); 79 | // } catch (InterruptedException e) { 80 | // e.printStackTrace(); 81 | // } 82 | // 83 | // ViewInteraction settingsTextView = onView( 84 | // allOf(withId(R.id.title), withText("Settings"), 85 | // isDisplayed())); 86 | // 87 | // assert(settingsTextView != null); 88 | // 89 | // } 90 | // 91 | // private static Matcher childAtPosition( 92 | // final Matcher parentMatcher, final int position) { 93 | // 94 | // return new TypeSafeMatcher() { 95 | // @Override 96 | // public void describeTo(Description description) { 97 | // description.appendText("Child at position " + position + " in parent "); 98 | // parentMatcher.describeTo(description); 99 | // } 100 | // 101 | // @Override 102 | // public boolean matchesSafely(View view) { 103 | // ViewParent parent = view.getParent(); 104 | // return parent instanceof ViewGroup && parentMatcher.matches(parent) 105 | // && view.equals(((ViewGroup) parent).getChildAt(position)); 106 | // } 107 | // }; 108 | // } 109 | //} 110 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/battery/UnofficialBatteryApi.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main.battery; 2 | 3 | 4 | import android.os.Build; 5 | 6 | 7 | import java.util.Locale; 8 | 9 | public class UnofficialBatteryApi { 10 | public static final String BUILD_MODEL = Build.MODEL.toLowerCase(Locale.ENGLISH); 11 | 12 | public static Integer getCurrent() { 13 | for (UnofficialBatteryMethod unofficialBatteryMethod : methods) { 14 | if (unofficialBatteryMethod.isApplicable(BUILD_MODEL)) { 15 | Integer val = unofficialBatteryMethod.read(); 16 | if (val != null) { 17 | return val; 18 | } 19 | } 20 | } 21 | return null; 22 | } 23 | 24 | public static final UnofficialBatteryMethod[] methods = { 25 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/current_max", 1.0F, null, null, new String[]{"gt-i9300", "gt-i9300T", "gt-i9305", "gt-i9305N", "gt-i9305T", "shv-e210k", "shv-e210l", "shv-e210s", "sgh-t999", "sgh-t999l", "sgh-t999v", "sgh-i747", "sgh-i747m", "sgh-n064", "sc-06d", "sgh-n035", "sc-03e", "SCH-j021", "scl21", "sch-r530", "sch-i535", "sch-S960l", "gt-i9308", "sch-i939", "sch-s968c"}), 26 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/current_now", 1.0F, null, null, new String[]{"nexus 7", "one", "lg-d851"}), 27 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/da9052-bat/current_avg", 1.0F, null, null, new String[]{"sl930"}), 28 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/current_now", 1.0F, null, null, new String[]{"sgh-i337", "gt-i9505", "gt-i9500", "sch-i545", "find 5", "sgh-m919", "sgh-i537"}), 29 | new UnofficialBatteryMethod(1, "/sys/devices/platform/mt6329-battery/FG_Battery_CurrentConsumption", 1.0F, null, null, new String[]{"cynus"}), 30 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/BatteryAverageCurrent", 1.0F, null, null, new String[]{"zp900", "jy-g3", "zp800", "zp800h", "zp810", "w100", "zte v987"}), 31 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/current_avg", 1.0F, null, null, new String[]{"gt-p31", "gt-p51"}), 32 | new UnofficialBatteryMethod(2, "/sys/class/power_supply/battery/batt_attr_text", 1.0F, "I_MBAT", "I_MBAT", new String[]{"htc one x"}), 33 | new UnofficialBatteryMethod(2, "/sys/class/power_supply/battery/smem_text", 1.0F, "eval_current", "batt_current", new String[]{"wildfire s"}), 34 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/current_now", 1.0F, null, null, new String[]{"triumph", "ls670", "gt-i9300", "sm-n9005", "gt-n7100", "sgh-i317"}), 35 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/batt_current", 1.0F, null, null, new String[]{"desire hd", "desire z", "inspire", "pg41200"}), 36 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/current_now", 0.1F, null, null, new String[]{"LG-D850", "LG-D851", "LG-D852", "LG-D855", "LG-D856", "LG-D858", "LG-D859"}), 37 | new UnofficialBatteryMethod(1, "/sys/devices/platform/ds2784-battery/getcurrent", 0.001F, null, null, new String[0]), 38 | new UnofficialBatteryMethod(1, "/sys/devices/platform/i2c-adapter/i2c-0/0-0036/power_supply/ds2746-battery/current_now", 1.0F, null, null, new String[0]), 39 | new UnofficialBatteryMethod(1, "/sys/devices/platform/i2c-adapter/i2c-0/0-0036/power_supply/battery/current_now", 1.0F, null, null, new String[0]), 40 | new UnofficialBatteryMethod(1, "/sys/devices/platform/tegra-i2c.4/i2c-4/4-0040/current1_input", 1.0F, null, null, new String[0]), 41 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/bq27425_battery/charge_now", 0.1F, null, null, new String[0]), 42 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/bq27541-bat/current_now", 1.0F, null, null, new String[0]), 43 | new UnofficialBatteryMethod(3, "/sys/class/power_supply/battery/smem_text", 1.0F, null, null, new String[0]), 44 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/batt_current", 1.0F, null, null, new String[0]), 45 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/current_now", 1.0F, null, null, new String[0]), 46 | new UnofficialBatteryMethod(2, "/sys/class/power_supply/battery/batt_attr_text", 1.0F, "batt_discharge_current", "batt_current", new String[0]), 47 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/batt_chg_current", 1.0F, null, null, new String[0]), 48 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/charger_current", 1.0F, null, null, new String[0]), 49 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/max17042-0/current_now", 1.0F, null, null, new String[0]), 50 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/bq27520/current_now", 1.0F, null, null, new String[0]), 51 | new UnofficialBatteryMethod(1, "/sys/devices/platform/cpcap_battery/power_supply/usb/current_now", 1.0F, null, null, new String[0]), 52 | new UnofficialBatteryMethod(1, "/sys/EcControl/BatCurrent", 1.0F, null, null, new String[0]), 53 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/batt_current_now", 1.0F, null, null, new String[0]), 54 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/batt_current_adc", 1.0F, null, null, new String[0]), 55 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/max170xx_battery/current_now", 0.001F, null, null, new String[0]), 56 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/ab8500_fg/current_now", 0.001F, null, null, new String[0]), 57 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/android-battery/current_now", 1.0F, null, null, new String[0]), 58 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/ds2784-fuelgauge/current_now", 0.001F, null, null, new String[0]), 59 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/Battery/current_now", 1.0F, null, null, new String[0]), 60 | new UnofficialBatteryMethod(1, "/sys/devices/platform/msm-charger/power_supply/battery_gauge/current_now", 1.0F, null, null, new String[0]), 61 | new UnofficialBatteryMethod(1, "/sys/devices/platform/battery/power_supply/battery/BatteryAverageCurrent", 1.0F, null, null, new String[0]), 62 | new UnofficialBatteryMethod(1, "/sys/devices/platform/mt6320-battery/power_supply/battery/BatteryAverageCurrent", 1.0F, null, null, new String[0]), 63 | new UnofficialBatteryMethod(1, "/sys/devices/platform/msm-battery/power_supply/battery/chg_current_adc", 1.0F, null, null, new String[0]), 64 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/bq27x41/current_now", 1.0F, null, null, new String[0]), 65 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/bq27541_battery/current_now", 1.0F, null, null, new String[0]), 66 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/cw2015_battery/current_now", 0.001F, null, null, new String[0]), 67 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/dollar_cove_battery/current_now", 0.001F, null, null, new String[0]), 68 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/bms/current_now", 1.0F, null, null, new String[0]), 69 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/current_avg", 1.0F, null, null, new String[0]), 70 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/BatteryAverageCurrent", 1.0F, null, null, new String[0]), 71 | new UnofficialBatteryMethod(1, "/sys/class/power_supply/battery/current_max", 1.0F, null, null, new String[0]) 72 | }; 73 | 74 | 75 | } 76 | -------------------------------------------------------------------------------- /app/src/main/res/layout/content_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 10 | 11 | 17 | 18 | 32 | 33 | 37 | 38 | 49 | 50 | 57 | 59 | 61 | 64 | 65 | 66 | 74 | 75 | 81 | 83 | 85 | 88 | 89 | 90 | 91 | 92 | 97 | 98 | 99 | 100 | 101 | 104 | 106 | 107 | 108 | 109 | 110 | 111 | 113 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 124 | 126 | 127 | 128 | 129 | 130 | 131 | 133 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 144 | 146 | 147 | 148 | 149 | 150 | 151 | 153 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 164 | 166 | 167 | 168 | 169 | 170 | 171 | 173 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 184 | 186 | 187 | 188 | 189 | 190 | 191 | 193 | 195 | 196 | 197 | 198 | 199 | -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main; 2 | 3 | 4 | import android.content.Intent; 5 | import android.content.SharedPreferences; 6 | import android.graphics.PorterDuff; 7 | import android.os.BatteryManager; 8 | import android.os.Build; 9 | import android.preference.PreferenceManager; 10 | import android.support.v7.app.AppCompatActivity; 11 | import android.os.Bundle; 12 | import android.support.v7.widget.Toolbar; 13 | import android.view.Menu; 14 | import android.view.MenuItem; 15 | import android.view.View; 16 | import android.view.ViewGroup; 17 | import android.widget.ProgressBar; 18 | import android.widget.TextView; 19 | 20 | import com.communitycode.amps.main.settings.SettingsActivity; 21 | 22 | import java.text.NumberFormat; 23 | import java.util.ArrayList; 24 | 25 | import static com.communitycode.amps.main.Utils.convertCelsiusToFahrenheit; 26 | import static com.communitycode.amps.main.Utils.flattenViewGroup; 27 | 28 | public class MainActivity extends AppCompatActivity implements BatteryInfoInterface { 29 | private BatteryPresenter mBatteryPresenter; 30 | 31 | private void findAndSetText(int id, String text) { 32 | if (text == null) { 33 | findAndSetText(id, R.string.blank_value); 34 | } 35 | else { 36 | ((TextView) findViewById(id)).setText(text); 37 | } 38 | } 39 | 40 | private void findAndSetText(int id, int resourceId) { 41 | ((TextView) findViewById(id)).setText(getResources().getString(resourceId)); 42 | } 43 | 44 | public void changeAccentColor(int colorResId) { 45 | int color = getResources().getColor(colorResId); 46 | final String ACCENT = getResources().getString(R.string.accent_tag); 47 | 48 | // update text views 49 | ArrayList views = flattenViewGroup((ViewGroup) findViewById(R.id.root_main_activity)); 50 | for (int i = 0 ; i < views.size(); i ++) { 51 | View view = views.get(i); 52 | if (view instanceof TextView) { 53 | TextView textView = (TextView) view; 54 | Object tag = textView.getTag(); 55 | if (tag != null && tag.equals(ACCENT)) { 56 | textView.setTextColor(color); 57 | } 58 | } 59 | } 60 | 61 | // update throbber 62 | ProgressBar throbber = findViewById(R.id.indeterminateBar); 63 | throbber.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN); 64 | } 65 | 66 | public void resetCurrentHistory(View view) { 67 | mBatteryPresenter.resetCurrentHistory(); 68 | } 69 | 70 | public void goToCurrentInformation(View view) { 71 | BatteryInfoAlertDialog fragment = new BatteryInfoAlertDialog(); 72 | getSupportFragmentManager() 73 | .beginTransaction() 74 | .add(fragment, "dialog") 75 | .commit(); 76 | } 77 | 78 | public void showAmpInfoButton(boolean visible) { 79 | TextView textView = findViewById(R.id.amp_info); 80 | int i = visible ? View.VISIBLE : View.INVISIBLE; 81 | textView.setVisibility(i); 82 | } 83 | 84 | @Override 85 | protected void onCreate(Bundle savedInstanceState) { 86 | super.onCreate(savedInstanceState); 87 | setContentView(R.layout.activity_main); 88 | 89 | PreferenceManager.setDefaultValues(this, R.xml.settings, false); 90 | 91 | Toolbar toolbar = findViewById(R.id.toolbar); 92 | setSupportActionBar(toolbar); 93 | 94 | 95 | 96 | findAndSetText(R.id.build_id_value, Build.ID); 97 | findAndSetText(R.id.android_version_value, Build.VERSION.RELEASE); 98 | findAndSetText(R.id.model_value, Build.MODEL); 99 | 100 | mBatteryPresenter = new BatteryPresenter(this, this); 101 | } 102 | 103 | 104 | @Override 105 | public boolean onCreateOptionsMenu(Menu menu) { 106 | // Inflate the menu; this adds items to the action bar if it is present. 107 | getMenuInflater().inflate(R.menu.menu_main, menu); 108 | return true; 109 | } 110 | 111 | @Override 112 | public boolean onOptionsItemSelected(MenuItem item) { 113 | // Handle action bar item clicks here. The action bar will 114 | // automatically handle clicks on the Home/Up button, so long 115 | // as you specify a parent activity in AndroidManifest.xml. 116 | int id = item.getItemId(); 117 | 118 | //noinspection SimplifiableIfStatement 119 | if (id == R.id.action_settings) { 120 | Intent intent = new Intent(this, SettingsActivity.class); 121 | startActivity(intent); 122 | return true; 123 | } 124 | 125 | 126 | return super.onOptionsItemSelected(item); 127 | } 128 | 129 | @Override 130 | protected void onResume() { 131 | super.onResume(); 132 | mBatteryPresenter.start(); 133 | } 134 | 135 | @Override 136 | protected void onPause() { 137 | super.onPause(); 138 | mBatteryPresenter.stop(); 139 | } 140 | 141 | @Override 142 | // value in milliamps 143 | public void setMaxAmps(Integer value) { 144 | if (value == null) { 145 | findAndSetText(R.id.max_value, R.string.blank_value); 146 | } 147 | else { 148 | findAndSetText(R.id.max_value, getString(R.string.mA, value)); 149 | } 150 | } 151 | 152 | @Override 153 | // value in milliamps 154 | public void setMinAmps(Integer value) { 155 | if (value == null) { 156 | findAndSetText(R.id.min_value, R.string.blank_value); 157 | } 158 | else { 159 | findAndSetText(R.id.min_value, getString(R.string.mA, value)); 160 | } 161 | } 162 | 163 | @Override 164 | // value in milliamps 165 | public void setCurrentAmps(Integer value) { 166 | if (value == null) { 167 | findAndSetText(R.id.amps_value, R.string.blank_value); 168 | } 169 | else { 170 | findAndSetText(R.id.amps_value, getString(R.string.mA, value)); 171 | } 172 | } 173 | 174 | @Override 175 | // value in volts 176 | public void setVoltage(Double value) { 177 | if (value == null) { 178 | findAndSetText(R.id.voltage_value, R.string.blank_value); 179 | } 180 | else { 181 | findAndSetText(R.id.voltage_value, getString(R.string.V, value)); 182 | } 183 | } 184 | 185 | @Override 186 | // value in Celsius 187 | public void setTemperature(Double value) { 188 | if (value == null) { 189 | findAndSetText(R.id.temperature_value, R.string.blank_value); 190 | } 191 | else { 192 | SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); 193 | boolean isCelsius = sharedPref.getBoolean("use_celsius", true); 194 | 195 | if (isCelsius) { 196 | findAndSetText(R.id.temperature_value, getString(R.string.degrees_c, value)); 197 | } 198 | else { 199 | value = convertCelsiusToFahrenheit(value); 200 | findAndSetText(R.id.temperature_value, getString(R.string.degrees_f, value)); 201 | } 202 | } 203 | } 204 | 205 | @Override 206 | // status one of BatteryManager.BATTERY_STATUS_* 207 | public void setChargingStatus(int status) { 208 | int statusLbl; 209 | 210 | switch (status) { 211 | case BatteryManager.BATTERY_STATUS_CHARGING: 212 | statusLbl = R.string.battery_status_charging; 213 | changeAccentColor(R.color.chargingAccent); 214 | break; 215 | 216 | case BatteryManager.BATTERY_STATUS_FULL: 217 | statusLbl = R.string.battery_status_full; 218 | changeAccentColor(R.color.fullAccent); 219 | break; 220 | 221 | case BatteryManager.BATTERY_STATUS_UNKNOWN: 222 | statusLbl = -1; 223 | break; 224 | 225 | case BatteryManager.BATTERY_STATUS_DISCHARGING: 226 | case BatteryManager.BATTERY_STATUS_NOT_CHARGING: 227 | default: 228 | statusLbl = R.string.battery_status_discharging; 229 | changeAccentColor(R.color.dischargingAccent); 230 | break; 231 | } 232 | 233 | if (statusLbl != -1) { 234 | findAndSetText(R.id.charging_status_value, statusLbl); 235 | } 236 | else { 237 | findAndSetText(R.id.charging_status_value, R.string.blank_value); 238 | } 239 | 240 | } 241 | 242 | @Override 243 | // plugged one of BatteryManager.BATTERY_PLUGGED_* 244 | public void setPluggedInStatus(int plugged) { 245 | int pluggedLbl; 246 | 247 | switch (plugged) { 248 | case BatteryManager.BATTERY_PLUGGED_WIRELESS: 249 | pluggedLbl = R.string.battery_plugged_wireless; 250 | break; 251 | 252 | case BatteryManager.BATTERY_PLUGGED_USB: 253 | pluggedLbl = R.string.battery_plugged_usb; 254 | break; 255 | 256 | case BatteryManager.BATTERY_PLUGGED_AC: 257 | pluggedLbl = R.string.battery_plugged_ac; 258 | break; 259 | 260 | default: 261 | pluggedLbl = R.string.battery_plugged_none; 262 | break; 263 | } 264 | 265 | findAndSetText(R.id.plugged_in_value, pluggedLbl); 266 | } 267 | 268 | @Override 269 | // health one of BatteryManager.BATTERY_HEALTH_* 270 | public void setBatteryHealth(int health) { 271 | int healthLbl = -1; 272 | 273 | switch (health) { 274 | case BatteryManager.BATTERY_HEALTH_COLD: 275 | healthLbl = R.string.battery_health_cold; 276 | break; 277 | 278 | case BatteryManager.BATTERY_HEALTH_DEAD: 279 | healthLbl = R.string.battery_health_dead; 280 | break; 281 | 282 | case BatteryManager.BATTERY_HEALTH_GOOD: 283 | healthLbl = R.string.battery_health_good; 284 | break; 285 | 286 | case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: 287 | healthLbl = R.string.battery_health_over_voltage; 288 | break; 289 | 290 | case BatteryManager.BATTERY_HEALTH_OVERHEAT: 291 | healthLbl = R.string.battery_health_overheat; 292 | break; 293 | 294 | case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE: 295 | healthLbl = R.string.battery_health_unspecified_failure; 296 | break; 297 | 298 | case BatteryManager.BATTERY_HEALTH_UNKNOWN: 299 | default: 300 | break; 301 | } 302 | 303 | if (healthLbl == -1) { 304 | findAndSetText(R.id.health_value, R.string.blank_value); 305 | } 306 | else { 307 | findAndSetText(R.id.health_value, healthLbl); 308 | } 309 | } 310 | 311 | @Override 312 | public void setBatteryPercent(Double value) { 313 | if (value == null) { 314 | findAndSetText(R.id.battery_level_value, R.string.blank_value); 315 | } 316 | else { 317 | findAndSetText(R.id.battery_level_value, NumberFormat.getPercentInstance().format(value)); 318 | } 319 | } 320 | 321 | @Override 322 | public void setBatteryTechnology(String value) { 323 | if (value == null) { 324 | findAndSetText(R.id.technology_value, R.string.blank_value); 325 | } 326 | else { 327 | findAndSetText(R.id.technology_value, value); 328 | } 329 | } 330 | } 331 | -------------------------------------------------------------------------------- /app/src/main/java/com/communitycode/amps/main/settings/UnofficialBatteryApiPreference.java: -------------------------------------------------------------------------------- 1 | package com.communitycode.amps.main.settings; 2 | 3 | 4 | import android.app.AlertDialog; 5 | import android.app.Dialog; 6 | import android.content.Context; 7 | import android.content.DialogInterface; 8 | import android.content.res.TypedArray; 9 | import android.os.Parcel; 10 | import android.os.Parcelable; 11 | import android.preference.DialogPreference; 12 | import android.support.v7.widget.LinearLayoutManager; 13 | import android.support.v7.widget.RecyclerView; 14 | import android.text.TextUtils; 15 | import android.util.AttributeSet; 16 | 17 | import com.communitycode.amps.main.R; 18 | import com.communitycode.amps.main.battery.BatteryMethodInterface; 19 | import com.communitycode.amps.main.battery.OfficialBatteryMethod; 20 | import com.communitycode.amps.main.battery.UnofficialBatteryApi; 21 | import com.communitycode.amps.main.battery.UnofficialBatteryMethod; 22 | 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | 26 | public class UnofficialBatteryApiPreference extends DialogPreference { 27 | 28 | private ArrayList mEntries; 29 | private ArrayList mEntryValues; 30 | private String mValue; 31 | private String mSummary; 32 | private int mClickedDialogEntryIndex; 33 | private boolean mValueSet; 34 | 35 | public UnofficialBatteryApiPreference(Context context, AttributeSet attrs) { 36 | super(context, attrs); 37 | 38 | mEntries = new ArrayList<>(); 39 | mEntryValues = new ArrayList<>(); 40 | 41 | BatteryMethodInterface official = new OfficialBatteryMethod(context); 42 | UnofficialBatteryMethodAdapter.MethodInfo defaultMethod = 43 | new UnofficialBatteryMethodAdapter.MethodInfo(context.getString(R.string.xdefault), 44 | official.read()); 45 | mEntries.add(defaultMethod); 46 | mEntryValues.add(BatteryMethodPickler.toJson(official)); 47 | 48 | for (UnofficialBatteryMethod method : distinct(filterApplicable(UnofficialBatteryApi.methods))) { 49 | mEntries.add(new UnofficialBatteryMethodAdapter.MethodInfo(method.filePath, method.read())); 50 | mEntryValues.add(BatteryMethodPickler.toJson(method)); 51 | } 52 | 53 | 54 | setPositiveButtonText(null); 55 | setNegativeButtonText(android.R.string.cancel); 56 | 57 | setDialogIcon(null); 58 | } 59 | 60 | public static List filterApplicable(UnofficialBatteryMethod[] methods) { 61 | ArrayList applicableMethods = new ArrayList<>(); 62 | for (UnofficialBatteryMethod method : methods) { 63 | if (method.isApplicable(UnofficialBatteryApi.BUILD_MODEL)) { 64 | applicableMethods.add(method); 65 | } 66 | } 67 | return applicableMethods; 68 | } 69 | 70 | public static List distinct(List methods) { 71 | ArrayList uniqueMethods = new ArrayList<>(); 72 | for(UnofficialBatteryMethod a : methods) { 73 | boolean found = false; 74 | for (UnofficialBatteryMethod b : uniqueMethods) { 75 | if (a.equalsIgnoreTransient(b)) { 76 | found = true; 77 | break; 78 | } 79 | } 80 | if (!found) { 81 | uniqueMethods.add(a); 82 | } 83 | } 84 | return uniqueMethods; 85 | } 86 | 87 | /** 88 | * Sets the value of the key. This should be one of the entries in 89 | * entry values. 90 | * 91 | * @param value The value to set for the key. 92 | */ 93 | public void setValue(String value) { 94 | // Always persist/notify the first time. 95 | final boolean changed = !TextUtils.equals(mValue, value); 96 | if (changed || !mValueSet) { 97 | mValue = value; 98 | mValueSet = true; 99 | persistString(value); 100 | if (changed) { 101 | notifyChanged(); 102 | } 103 | } 104 | } 105 | 106 | /** 107 | * Returns the summary of this ListPreference. If the summary 108 | * has a {@linkplain java.lang.String#format String formatting} 109 | * marker in it (i.e. "%s" or "%1$s"), then the current entry 110 | * value will be substituted in its place. 111 | * 112 | * @return the summary with appropriate string substitution 113 | */ 114 | @Override 115 | public CharSequence getSummary() { 116 | final UnofficialBatteryMethodAdapter.MethodInfo entry = getEntry(); 117 | if (mSummary == null) { 118 | return super.getSummary(); 119 | } else { 120 | return String.format(mSummary, entry == null ? "" : entry.name); 121 | } 122 | } 123 | 124 | /** 125 | * Sets the summary for this Preference with a CharSequence. 126 | * If the summary has a 127 | * {@linkplain java.lang.String#format String formatting} 128 | * marker in it (i.e. "%s" or "%1$s"), then the current entry 129 | * value will be substituted in its place when it's retrieved. 130 | * 131 | * @param summary The summary for the preference. 132 | */ 133 | @Override 134 | public void setSummary(CharSequence summary) { 135 | super.setSummary(summary); 136 | if (summary == null && mSummary != null) { 137 | mSummary = null; 138 | } else if (summary != null && !summary.equals(mSummary)) { 139 | mSummary = summary.toString(); 140 | } 141 | } 142 | 143 | /** 144 | * Returns the value of the key. This should be one of the entries in 145 | * entry values 146 | * 147 | * @return The value of the key. 148 | */ 149 | public String getValue() { 150 | return mValue; 151 | } 152 | 153 | /** 154 | * Returns the entry corresponding to the current value. 155 | * 156 | * @return The entry corresponding to the current value, or null. 157 | */ 158 | public UnofficialBatteryMethodAdapter.MethodInfo getEntry() { 159 | int index = getValueIndex(); 160 | return index >= 0 && mEntries != null ? mEntries.get(index) : null; 161 | } 162 | 163 | /** 164 | * Returns the index of the given value (in the entry values array). 165 | * 166 | * @param value The value whose index should be returned. 167 | * @return The index of the value, or -1 if not found. 168 | */ 169 | public int findIndexOfValue(String value) { 170 | if (value != null && mEntryValues != null) { 171 | for (int i = mEntryValues.size() - 1; i >= 0; i--) { 172 | if (mEntryValues.get(i).equals(value)) { 173 | return i; 174 | } 175 | } 176 | } 177 | return -1; 178 | } 179 | 180 | private int getValueIndex() { 181 | return findIndexOfValue(mValue); 182 | } 183 | 184 | @Override 185 | protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { 186 | super.onPrepareDialogBuilder(builder); 187 | 188 | if (mEntries == null || mEntryValues == null) { 189 | throw new IllegalStateException( 190 | "ListPreference requires an entries array and an entryValues array."); 191 | } 192 | 193 | Context context = builder.getContext(); 194 | RecyclerView mRecyclerView = new RecyclerView(context); 195 | RecyclerView.LayoutManager mLayoutManager; 196 | 197 | // use a linear layout manager 198 | mLayoutManager = new LinearLayoutManager(context); 199 | mRecyclerView.setLayoutManager(mLayoutManager); 200 | 201 | // specify an adapter (see also next example) 202 | mClickedDialogEntryIndex = getValueIndex(); 203 | final UnofficialBatteryMethodAdapter mAdapter = new UnofficialBatteryMethodAdapter(mEntries, mClickedDialogEntryIndex); 204 | mAdapter.setOnClickListener(new UnofficialBatteryMethodAdapter.OnClickHandler() { 205 | public void onClick(int position) { 206 | UnofficialBatteryApiPreference that = UnofficialBatteryApiPreference.this; 207 | 208 | mAdapter.setCheckedPosition(position); 209 | that.mClickedDialogEntryIndex = that.mClickedDialogEntryIndex == position ? -1 : position; 210 | 211 | Dialog dialog = that.getDialog(); 212 | that.onClick(dialog, DialogInterface.BUTTON_POSITIVE); 213 | dialog.dismiss(); 214 | } 215 | }); 216 | mRecyclerView.setAdapter(mAdapter); 217 | builder.setView(mRecyclerView); 218 | 219 | builder.setPositiveButton(null, null); 220 | } 221 | 222 | 223 | @Override 224 | protected void onDialogClosed(boolean positiveResult) { 225 | super.onDialogClosed(positiveResult); 226 | 227 | if (positiveResult && mClickedDialogEntryIndex >= 0 && mEntryValues != null) { 228 | String value = mEntryValues.get(mClickedDialogEntryIndex); 229 | if (callChangeListener(value)) { 230 | setValue(value); 231 | } 232 | } 233 | // Same item was clicked. Deselect item 234 | if (positiveResult && mClickedDialogEntryIndex == -1) { 235 | String value = null; 236 | if (callChangeListener(value)) { 237 | setValue(value); 238 | } 239 | } 240 | } 241 | 242 | @Override 243 | protected Object onGetDefaultValue(TypedArray a, int index) { 244 | return a.getString(index); 245 | } 246 | 247 | @Override 248 | protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { 249 | setValue(restoreValue ? getPersistedString(mValue) : (String) defaultValue); 250 | } 251 | 252 | @Override 253 | protected Parcelable onSaveInstanceState() { 254 | final Parcelable superState = super.onSaveInstanceState(); 255 | if (isPersistent()) { 256 | // No need to save instance state since it's persistent 257 | return superState; 258 | } 259 | 260 | final SavedState myState = new SavedState(superState); 261 | myState.value = getValue(); 262 | return myState; 263 | } 264 | 265 | @Override 266 | protected void onRestoreInstanceState(Parcelable state) { 267 | if (state == null || !state.getClass().equals(SavedState.class)) { 268 | // Didn't save state for us in onSaveInstanceState 269 | super.onRestoreInstanceState(state); 270 | return; 271 | } 272 | 273 | SavedState myState = (SavedState) state; 274 | super.onRestoreInstanceState(myState.getSuperState()); 275 | setValue(myState.value); 276 | } 277 | 278 | private static class SavedState extends BaseSavedState { 279 | String value; 280 | 281 | public SavedState(Parcel source) { 282 | super(source); 283 | value = source.readString(); 284 | } 285 | 286 | @Override 287 | public void writeToParcel(Parcel dest, int flags) { 288 | super.writeToParcel(dest, flags); 289 | dest.writeString(value); 290 | } 291 | 292 | public SavedState(Parcelable superState) { 293 | super(superState); 294 | } 295 | 296 | public static final Parcelable.Creator CREATOR = 297 | new Parcelable.Creator() { 298 | public SavedState createFromParcel(Parcel in) { 299 | return new SavedState(in); 300 | } 301 | 302 | public SavedState[] newArray(int size) { 303 | return new SavedState[size]; 304 | } 305 | }; 306 | } 307 | } -------------------------------------------------------------------------------- /license.md: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | ========================== 3 | Version 3, 29 June 2007 4 | ========================== 5 | 6 | > Copyright (C) 2007 Free Software Foundation, Inc. 7 | Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. 8 | 9 | # Preamble 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | # TERMS AND CONDITIONS 72 | 73 | ## 0. Definitions. 74 | 75 | _"This License"_ refers to version 3 of the GNU General Public License. 76 | 77 | _"Copyright"_ also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | _"The Program"_ refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as _"you"_. _"Licensees"_ and 82 | "recipients" may be individuals or organizations. 83 | 84 | To _"modify"_ a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a _"modified version"_ of the 87 | earlier work or a work _"based on"_ the earlier work. 88 | 89 | A _"covered work"_ means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To _"propagate"_ a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To _"convey"_ a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | ## 1. Source Code. 113 | 114 | The _"source code"_ for a work means the preferred form of the work 115 | for making modifications to it. _"Object code"_ means any non-source 116 | form of a work. 117 | 118 | A _"Standard Interface"_ means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The _"System Libraries"_ of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The _"Corresponding Source"_ for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | ## 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | ## 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | ## 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | ## 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | ## 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A _"User Product"_ is either (1) a _"consumer product"_, which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | _"Installation Information"_ for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | ## 7. Additional Terms. 344 | 345 | _"Additional permissions"_ are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | ## 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | ## 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | ## 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An _"entity transaction"_ is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | ## 11. Patents. 472 | 473 | A _"contributor"_ is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's _"essential patent claims"_ are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | ## 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | ## 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | ## 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | ## 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | ## 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | ## 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | # END OF TERMS AND CONDITIONS 622 | -------------------------------------------------------------------------- 623 | 624 | 625 | # How to Apply These Terms to Your New Programs 626 | 627 | If you develop a new program, and you want it to be of the greatest 628 | possible use to the public, the best way to achieve this is to make it 629 | free software which everyone can redistribute and change under these terms. 630 | 631 | To do so, attach the following notices to the program. It is safest 632 | to attach them to the start of each source file to most effectively 633 | state the exclusion of warranty; and each file should have at least 634 | the "copyright" line and a pointer to where the full notice is found. 635 | 636 | 637 | Copyright (C) 638 | 639 | This program is free software: you can redistribute it and/or modify 640 | it under the terms of the GNU General Public License as published by 641 | the Free Software Foundation, either version 3 of the License, or 642 | (at your option) any later version. 643 | 644 | This program is distributed in the hope that it will be useful, 645 | but WITHOUT ANY WARRANTY; without even the implied warranty of 646 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 647 | GNU General Public License for more details. 648 | 649 | You should have received a copy of the GNU General Public License 650 | along with this program. If not, see . 651 | 652 | Also add information on how to contact you by electronic and paper mail. 653 | 654 | If the program does terminal interaction, make it output a short 655 | notice like this when it starts in an interactive mode: 656 | 657 | Copyright (C) 658 | This program comes with ABSOLUTELY NO WARRANTY; for details type 'show w'. 659 | This is free software, and you are welcome to redistribute it 660 | under certain conditions; type 'show c' for details. 661 | 662 | The hypothetical commands _'show w'_ and _'show c'_ should show the appropriate 663 | parts of the General Public License. Of course, your program's commands 664 | might be different; for a GUI interface, you would use an "about box". 665 | 666 | You should also get your employer (if you work as a programmer) or school, 667 | if any, to sign a "copyright disclaimer" for the program, if necessary. 668 | For more information on this, and how to apply and follow the GNU GPL, see 669 | . 670 | 671 | The GNU General Public License does not permit incorporating your program 672 | into proprietary programs. If your program is a subroutine library, you 673 | may consider it more useful to permit linking proprietary applications with 674 | the library. If this is what you want to do, use the GNU Lesser General 675 | Public License instead of this License. But first, please read 676 | . 677 | --------------------------------------------------------------------------------