Skip to content

Commit

Permalink
Initial push
Browse files Browse the repository at this point in the history
The project was just converted from Eclipse to Android Studio, so
there may be some glitches.
  • Loading branch information
fornwall committed Jul 26, 2015
1 parent 9e043ef commit 1f2e562
Show file tree
Hide file tree
Showing 38 changed files with 2,150 additions and 2 deletions.
39 changes: 39 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# .gitignore from https://gist.github.com/iainconnor/8605514:

# Built application files
build/

# Crashlytics configuations
com_crashlytics_export_strings.xml

# Local configuration file (sdk path, etc)
local.properties

# Gradle generated files
.gradle/

# Signing files
.signing/

# User-specific configurations
.idea/libraries/
.idea/workspace.xml
.idea/tasks.xml
.idea/.name
.idea/compiler.xml
.idea/copyright/profiles_settings.xml
.idea/encodings.xml
.idea/misc.xml
.idea/modules.xml
.idea/scopes/scope_settings.xml
.idea/vcs.xml
*.iml

# OS-specific files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
18 changes: 18 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
# termux-api
Add-on app which exposes device functionality as API to command line programs.
Termux API
==========
This is an app exposing Android API to command line usage and scripts or programs.

The termux-api client helper binary
===================================
The client helper binary ([termux-api.c](https://github.com/termux/termux-packages/blob/master/packages/termux-api/termux-api.c))
generates two linux anonymous namespace sockets, and passes their address as in:
- /system/bin/am broadcast ${SERVICE_CLASS} --es socket_input ${INPUT_SOCKET} --es socket_output ${OUTPUT_SOCKET}
where the sockets are used to transfer:
- input through stdin to the helper binary are forwarded to java code
- java code may output feedback which are forwarded to the stdout of the helper binary

Client scripts
==============
Client scripts which processes command line arguments before calling the termux-api helper binary are available:
- [The termux-api package](https://github.com/termux/termux-packages/tree/master/packages/termux-api)
33 changes: 33 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion "22.0.1"

defaultConfig {
applicationId "com.termux.api"
minSdkVersion 21
targetSdkVersion 22
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}

signingConfigs {
release {
storeFile new File(RELEASE_STORE_FILE)
storePassword RELEASE_STORE_PASSWORD
keyAlias RELEASE_KEY_ALIAS
keyPassword RELEASE_KEY_PASSWORD
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.release
}
}
}
44 changes: 44 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.termux.api"
android:sharedUserId="com.termux"
android:versionCode="2"
android:versionName="0.2" >

<uses-feature android:name="android.hardware.touchscreen" android:required="false" />

<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="22" />

<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<!-- Resolve phone numbers to contact names: -->
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />

<!-- Some of the used permissions imply uses-feature, so we need to make it optional.
See http://developer.android.com/guide/topics/manifest/uses-feature-element.html#permissions -->
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<uses-feature android:name="android.hardware.location" android:required="false" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />
<uses-feature android:name="android.hardware.microphone" android:required="false" />
<uses-feature android:name="android.hardware.telephony" android:required="false" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Material.Light" >
<receiver android:name="com.termux.api.TermuxApiReceiver"/>
<activity android:name="com.termux.api.PhotoActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:exported="false"/>
<activity android:name="com.termux.api.DialogActivity" android:theme="@android:style/Theme.Material.Light.Dialog.Alert" android:noHistory="true" android:excludeFromRecents="true" android:exported="false"/>
<service android:name="com.termux.api.SpeechToTextAPI$SpeechToTextService"/>
<service android:name="com.termux.api.TextToSpeechAPI$TextToSpeechService" />
</application>

</manifest>
111 changes: 111 additions & 0 deletions app/src/main/java/com/termux/api/BatteryStatusAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.termux.api;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.util.JsonWriter;

import com.termux.api.util.ResultReturner;
import com.termux.api.util.TermuxApiLogger;
import com.termux.api.util.ResultReturner.ResultJsonWriter;

public class BatteryStatusAPI {

public static void onReceive(TermuxApiReceiver apiReceiver, final Context context, Intent intent) {
ResultReturner.returnData(apiReceiver, intent, new ResultJsonWriter() {
@Override
public void writeJson(JsonWriter out) throws Exception {
Intent batteryStatus = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
final int batteryPercentage = (level * 100) / scale;

int health = batteryStatus.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
String batteryHealth;
switch (health) {
case BatteryManager.BATTERY_HEALTH_COLD:
batteryHealth = "COLD";
break;
case BatteryManager.BATTERY_HEALTH_DEAD:
batteryHealth = "DEAD";
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
batteryHealth = "GOOD";
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
batteryHealth = "OVERHEAD";
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
batteryHealth = "OVER_VOLTAGE";
break;
case BatteryManager.BATTERY_HEALTH_UNKNOWN:
batteryHealth = "UNKNOWN";
break;
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
batteryHealth = "UNSPECIFIED_FAILURE";
break;
default:
batteryHealth = Integer.toString(health);
}

// BatteryManager.EXTRA_PLUGGED: "Extra for ACTION_BATTERY_CHANGED: integer indicating whether the
// device is plugged in to a power source; 0 means it is on battery, other constants are different types
// of power sources."
int pluggedInt = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
String batteryPlugged;
switch (pluggedInt) {
case 0:
batteryPlugged = "UNPLUGGED";
break;
case BatteryManager.BATTERY_PLUGGED_AC:
batteryPlugged = "PLUGGED_AC";
break;
case BatteryManager.BATTERY_PLUGGED_USB:
batteryPlugged = "PLUGGED_USB";
break;
case BatteryManager.BATTERY_PLUGGED_WIRELESS:
batteryPlugged = "PLUGGED_WIRELESS";
break;
default:
batteryPlugged = "PLUGGED_" + pluggedInt;
}

double batteryTemperature = batteryStatus.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1) / 10.f;

String batteryStatusString;
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
switch (status) {
case BatteryManager.BATTERY_STATUS_CHARGING:
batteryStatusString = "CHARGING";
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
batteryStatusString = "DISCHARGING";
break;
case BatteryManager.BATTERY_STATUS_FULL:
batteryStatusString = "FULL";
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
batteryStatusString = "NOT_CHARGING";
break;
case BatteryManager.BATTERY_STATUS_UNKNOWN:
batteryStatusString = "UNKNOWN";
break;
default:
TermuxApiLogger.error("Invalid BatteryManager.EXTRA_STATUS value: " + status);
batteryStatusString = "UNKNOWN";
}

out.beginObject();
out.name("health").value(batteryHealth);
out.name("percentage").value(batteryPercentage);
out.name("plugged").value(batteryPlugged);
out.name("status").value(batteryStatusString);
out.name("temperature").value(batteryTemperature);
out.endObject();
}
});

}
}
Loading

0 comments on commit 1f2e562

Please sign in to comment.