Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New sim api #601

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/src/main/java/com/termux/api/TermuxApiReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.termux.api.apis.SAFAPI;
import com.termux.api.apis.SensorAPI;
import com.termux.api.apis.ShareAPI;
import com.termux.api.apis.SimAPI;
import com.termux.api.apis.SmsInboxAPI;
import com.termux.api.apis.SmsSendAPI;
import com.termux.api.apis.SpeechToTextAPI;
Expand Down Expand Up @@ -199,6 +200,11 @@ private void doWork(Context context, Intent intent) {
case "Share":
ShareAPI.onReceive(this, context, intent);
break;
case "Sim":
if (TermuxApiPermissionActivity.checkAndRequestPermissions(context, intent, Manifest.permission.READ_PHONE_STATE)) {
SimAPI.onReceive(this, context, intent);
}
break;
case "SmsInbox":
if (TermuxApiPermissionActivity.checkAndRequestPermissions(context, intent, Manifest.permission.READ_SMS, Manifest.permission.READ_CONTACTS)) {
SmsInboxAPI.onReceive(this, context, intent);
Expand Down
122 changes: 122 additions & 0 deletions app/src/main/java/com/termux/api/apis/SimAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.termux.api.apis;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.telephony.SubscriptionManager;
import android.telephony.SubscriptionInfo;
import android.content.pm.PackageManager;
import android.se.omapi.SEService;
import android.se.omapi.SEService.OnConnectedListener;
import android.se.omapi.Reader;
import android.util.JsonWriter;

import androidx.annotation.RequiresPermission;
import androidx.appcompat.app.AppCompatActivity;

import com.termux.api.TermuxApiReceiver;
import com.termux.api.util.ResultReturner;
import com.termux.shared.logger.Logger;

import java.util.concurrent.Executor;
import java.io.PrintWriter;
import java.io.StringWriter;

public class SimAPI {

private static final String LOG_TAG = "SimAPI";
static Context context;
static JsonWriter out;
static SEService service;
static TermuxApiReceiver receiver;
static Intent intent;
static StringWriter sw;

public static void onReceive(TermuxApiReceiver _receiver, final Context _context, Intent _intent) {
Logger.logDebug(LOG_TAG, "onReceive");
context = _context;
receiver = _receiver;
intent = _intent;
sw = new StringWriter();
out = new JsonWriter(sw);

out.setIndent(" ");

try {
out.beginArray();
readSubscription();
readPm();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
readOmapi();

Logger.logInfo(LOG_TAG, sw.toString());
} catch (Exception e) {
Logger.logError(LOG_TAG, e.getMessage());
}
}

static void write() {
ResultReturner.returnData(context.getApplicationContext(), intent, new ResultReturner.WithInput() {
@Override public void writeResult(PrintWriter _out) throws Exception {
try {
_out.print(sw);
} catch (Exception e) {
Logger.logError(LOG_TAG, e.getMessage());
}
}
});
}

static void readPm() throws Exception {
out.beginObject();
final PackageManager pm = context.getPackageManager();
if(pm != null)
out.name("omapi").value (pm.hasSystemFeature(PackageManager.FEATURE_SE_OMAPI_UICC));
out.endObject();
}

static void readOmapi() throws Exception {
Executor exec = new Executor() {
public void execute(Runnable r) {
r.run();
}
};

OnConnectedListener listener = new OnConnectedListener() {
@Override public void onConnected() {
Logger.logInfo(LOG_TAG, "connected");
try {
out.beginObject();
Reader[] readers = service.getReaders();
for (Reader reader : readers) {
out.name ("name").value(reader.getName());
out.name("status").value(reader.isSecureElementPresent());
}
if (readers.length == 0) {
out.name("omapi").value("No reader available");
}
out.endObject();
out.endArray();
write();
} catch (Exception e) {
Logger.logError(LOG_TAG, e.getMessage());
}
}
};

service = new SEService(context.getApplicationContext(), exec, listener);
}

static void readSubscription() throws Exception {
out.beginObject();
SubscriptionManager sm = context.getSystemService(SubscriptionManager.class);
for(SubscriptionInfo si: sm.getActiveSubscriptionInfoList()) {
out.name("slot").value(si.getSimSlotIndex());
out.name("id").value(si.getSubscriptionId());
out.name("name").value(si.getCarrierName().toString());
}
out.endObject();
}
}