I have implemented below given to unlock my app (this code works for only systems apps so I have done my app as system app )
TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
int state = manager.getSimState();
if(state == TelephonyManager.SIM_STATE_PIN_REQUIRED || state == TelephonyManager.SIM_STATE_PUK_REQUIRED)
{
try {
#SuppressWarnings("rawtypes")
Class clazz = Class.forName(manager.getClass().getName());
Method m = clazz.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony it = (ITelephony) m.invoke(manager);
if (it.supplyPin(simPin)) {
Toast.makeText(context,"SIM UnLocked",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context,"SIM UNLOCK FAILED",Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
//
e.printStackTrace();
}
}else{
Toast.makeText(context,"SIM is not Locked",Toast.LENGTH_LONG).show();
}
It works fine for me, but now I need to implement setting or resetting SIM PIN programmatically, let me know if it is possible or not. if possible than how can I implement that?
String ussdCode = "**04*"+oldPin+"*"+newPin+"*"+newPin+"#";
startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussdCode)));
Related
I have one very unusual case when calling an Android hidden API over reflection freezes the calling thread eternally. I cannot debug the underlying code as it is a code from Android itself normally not visible. I tried running the code in an asynctask, in a normal thread but nor asynctask.cancel nor thread.interrupt kills the thread, the thread stays visible, I can see it while debugging. Is it really not possible to run a code encapsulated and kill it completely eventually?
This problems occurs only on Android O and only on some devices, that's why I need to test-run it to see if it works on the current device and be able to activate a feature according to that. The code below, I don't really see a solution for this:
Thread endCallThread;
Runnable myRunnable;
private void checkFeatureSupport(final Context context) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
myRunnable = new Runnable() {
#Override
public void run() {
doInBackgroundWrapper();
if (getActivity() != null) {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
//handleResult();
}
});
}
}
};
endCallThread = new Thread(myRunnable, "endCallThread");
endCallThread.start();
new CountDownTimer(3000, 3000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Log.e("TESTAUTOMATION", "endCall did not finished in 3 seconds");
// stop async task if not in progress
if (endCallThread.isAlive()) {
try {
endCallThread.interrupt();
endCallThread = null;
myRunnable = null;
System.gc();
Log.e("TESTAUTOMATION", "endCallThread interrupted");
} catch (Exception ex) {
Log.e("TESTAUTOMATION", "endCallThread interrupted exception");
}
//handleResult();
}
}
}.start();
} else {
mEndCallSupported = true;
}
}
}
private void doInBackgroundWrapper() {
try {
if (getContext() == null) {
return;
}
final TelephonyManager telMan = (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
if (telMan == null) {
return;
}
final Class<?> classTemp = Class.forName(telMan.getClass().getName());
final Method methodTemp = classTemp.getDeclaredMethod("getITelephony");
methodTemp.setAccessible(true);
ITelephony telephonyService = (ITelephony) methodTemp.invoke(telMan);
// If this call does not return a security exception we say the call principally works.
Log.d("TESTAUTOMATION", "endCall before");
// This call endCall may freeze depending on device, mostly seen on Nexus 5x with Android 8&8.1
telephonyService.endCall();
Log.d("TESTAUTOMATION", "endCall after");
mSupported = true;
} catch (Exception ex) {
ex.printStackTrace();
mSupported = false;
}
}
To reproduce this the device should better no have a SIM inserted.
Thread.interrupt() in a common case does not stop a thread, it just marks specific boolean field (isInterrupted) as true. If a developer wants to stop thread's work at some point (after calling Thread.interrupt()) he can rely on this boolean filed Thread.isInterrupted() when he is implementing some work in a separate thread.
So I guess there is no such checking in the reflected hidden method what you are trying to call.
To stop your thread you can try deprecated Thread.stop() but it is a really bad practice.
We have 2 cisco phones: one for call manager and another for his superviser.
We need to create a conference when the manager answers and put the supervisor's phone on mute. We are trying to achieve it using JTApi: wait for event TermConnActiveEv, then trying to create conference.
Here is the code sample.
if (callEv instanceof TermConnActiveEv) {
CiscoCall thisCall = (CiscoCall) callEv.getCall();
TerminalConnection connection = ((TermConnActiveEv) callEv).getTerminalConnection();
if (thisCall.getState() != Call.ACTIVE)
{
System.out.println("call is not active");
return;
}
try {
CiscoCall newCall = (CiscoCall) provider.createCall();
newCall.consult(connection);
newCall.conference(thisCall);
....
However, PreConditionException is thrown. What are we doing wrong?
You don't need to use Barge to create a conference.
You can try to do something like that:
if (callEv instanceof TermConnActiveEv) {
CiscoCall thisCall = (CiscoCall) callEv.getCall();
TerminalConnection tc = thisCall.getConferenceController();
Connection[] connections = thisCall.getConnections();
TerminalConnection[] tcs = connections[0].getTerminalConnections();
if (tcs.length > 0 && tc == null) {
tc = tcs[0];
}
if (tc == null) {
System.out.println("Conference controller is null.");
} else {
try {
Call call = provider.createCall();
call.connect(thisAddress.getTerminals()[0], thisAddress, superVisorAddress);
thisCall.conference(call);
} catch (Exception ex) {
System.out.println("Exception " + ex);
ex.printStackTrace();
}
}
}
To set mute you can use:
((CiscoTerminal)termConnections[i].getTerminal().sendData("<CiscoIPPhoneExecute><ExecuteItem Priority=\"0\"URL=\"Key:Mute\"/></CiscoIPPhoneExecute>");
Before the application can make use of this feature, it must add TerminalObserver on the terminal.
I am unlocking SIM PIN dynamically through my app via these lines
TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
int state = manager.getSimState();
if(state == TelephonyManager.SIM_STATE_PIN_REQUIRED || state == TelephonyManager.SIM_STATE_PUK_REQUIRED)
{
try {
#SuppressWarnings("rawtypes")
Class clazz = Class.forName(manager.getClass().getName());
Method m = clazz.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony it = (ITelephony) m.invoke(manager);
if (it.supplyPin(simPin)) {
Toast.makeText(context,"SIM UnLocked",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context,"SIM UNLOCK FAILED",Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
//
e.printStackTrace();
}
}else{
Toast.makeText(context,"SIM is not Locked",Toast.LENGTH_LONG).show();
}
now I want to know how much number of tries already made to unlock the SIM like default SIM PIN/PUK unlocker tells"two attempts left" . let me know if there is any possibility.
Just create an int that counts from 3 to 0 if sim unlock fails..
int attempts = 3;
} else {
Toast.makeText(context,"SIM UNLOCK FAILED",Toast.LENGTH_LONG).show();
attempts --;
}
My app has main.xml and MainActivity. I wanted to convert it to a tabbed styled app. By the help of this tutorial, I've successfully made a UI with a tabbed style.
Now I have to put the codes in my MainActivty(not tabbed style) to the fragments. i don't know how to do it. when i just put the codes to my ToolsFragment.java, it doesn't work.
Here are my codes:
//To get ip address using netcfg
private String ipnc()
{
int e = doNETCFG().indexOf("10.");
if (e == -1)
{
return "";
}
else
{
String ipnc1 = doNETCFG().substring(e, e + 15);
String ipnc2[] = ipnc1.split("/");
String ipnc3 = ipnc2[0];
return ipnc3;
}
}
//To generate netcfg from command line
public String doNETCFG()
{
String str = null;
try
{
Process localProcess = Runtime.getRuntime().exec("/system/bin/netcfg");
BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localProcess.getInputStream()));
char[] arrayOfChar = new char[4096];
StringBuffer localStringBuffer = new StringBuffer();
while (true)
{
int i = localBufferedReader.read(arrayOfChar);
if (i <= 0)
{
localBufferedReader.close();
localProcess.waitFor();
str = localStringBuffer.toString();
break;
}
localStringBuffer.append(arrayOfChar, 0, i);
}
}
catch (IOException localIOException)
{
Log.e("TAG", localIOException.getStackTrace().toString());
}
catch (InterruptedException localInterruptedException)
{
Log.e("TAG", localInterruptedException.getStackTrace().toString());
}
return str;
}
//To enable/disable mobile data
private void setMobileDataEnabled(Context context, boolean enabled)
{
final ConnectivityManager conman;
conman =
(ConnectivityManager)context.getSystemService
(Context.CONNECTIVITY_SERVICE);
final Class conmanClass;
try
{
conmanClass =
Class.forName(conman.getClass
().getName());
final Field
iConnectivityManagerField =
conmanClass.getDeclaredField
("mService");
iConnectivityManagerField.
setAccessible(true);
final Object
iConnectivityManager =
iConnectivityManagerField.get
(conman);
final Class
iConnectivityManagerClass =
Class.forName
(iConnectivityManager.getClass
().getName());
final Method
setMobileDataEnabledMethod =
iConnectivityManagerClass.
getDeclaredMethod
("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.
setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}
catch
(ClassNotFoundException e)
{
e.printStackTrace();
}
catch
(InvocationTargetException e)
{
e.printStackTrace();
}
catch
(NoSuchMethodException e)
{
e.printStackTrace();
}
catch
(IllegalAccessException e)
{
e.printStackTrace();
}
catch
(NoSuchFieldException e)
{
e.printStackTrace();
}
}
//To play success alert tone
public void playAlertTone()
{
new Thread()
{
public void run()
{
int i = 0;
while (true)
{
if (i >= 1)
return;
MediaPlayer localMediaPlayer = MediaPlayer.create(getApplicationContext(), 0x7f040000);
localMediaPlayer.start();
i++;
try
{
Thread.sleep(100 + localMediaPlayer.getDuration());
localMediaPlayer.release();
}
catch (InterruptedException localInterruptedException)
{
}
}
}
}
.start();
}
What should I do? Do I have to put this into another activity and call it in fragment? If that's the case, how?
Or convert it to a code executable in fragments and run it there? How can I do this?
I'm a newbie in android programming. Thanks!
when you convert an application to fragment based structure, you have to consider one thing that the context is same for all the fragments in an activity. You will get it by calling getActivity(). So save your context first and use it where ever you want the context.
From your question you are developing an application with tabs. So you may have to create fragments as many as the tabs. You can put your code for each tab in the corresponding fragment.
Now to communicate between the fragments the best way is to use callbacks in your parent activity.
A simple tutorial for implementing fragments can be found HERE
The communication between fragments is explained HERE
Fragments do most of the things activities do. The reason why they were introduced (one of the reasons at least) was so you can place the code in them instead of in the activities of your app. If you think this way, you will notice that you will only need a small number of activities.
I am not sure what you mean by "convert it to a code executable in fragments". You can place your code inside your fragments (use methods) and then simply call those methods.
If you are not sure how Fragments work, I would highly recommend reading the documentation.
I hope this gives you an idea of how to get your code to work in fragments.
Is there any way to find out bluetooth tethering is enabled or disabled programmatically in Android 2.3+(any version after 2.3+)?
I am not asking to enable/disable it, but only to know if it is enabled currently or not.
It turns out that BluetoothPan (Personal Area Networking) is the keyword for tethering. I pored over the API documentation and Android source code, but the brief examples in the comments were misleading. This poster provided an example but I had trouble with it initially:
Android BluetoothPAN to create TCP/IP network between Android device and Windows7 PC
I tried various other methods including checking the IP address of the BT device. However no Bluetooth network device exists, so there's no IP to check.
Detect USB tethering on android
Back to the BluetoothPan code... The example in the first thread was incomplete (no ServiceListener implementation). I tried a standard one but the isTetheringOn proxy call failed. The crucial piece is that the onServiceConnected() callback needs at least one line of code or the compiler optimizes it away. It also shouldn't disconnect the proxy like most other examples have. Here's the working code:
BluetoothAdapter mBluetoothAdapter = null;
Class<?> classBluetoothPan = null;
Constructor<?> BTPanCtor = null;
Object BTSrvInstance = null;
Class<?> noparams[] = {};
Method mIsBTTetheringOn;
#Override
public void onCreate() {
Context MyContext = getApplicationContext();
mBluetoothAdapter = getBTAdapter();
try {
classBluetoothPan = Class.forName("android.bluetooth.BluetoothPan");
mIsBTTetheringOn = classBluetoothPan.getDeclaredMethod("isTetheringOn", noparams);
BTPanCtor = classBluetoothPan.getDeclaredConstructor(Context.class, BluetoothProfile.ServiceListener.class);
BTPanCtor.setAccessible(true);
BTSrvInstance = BTPanCtor.newInstance(MyContext, new BTPanServiceListener(MyContext));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private BluetoothAdapter getBTAdapter() {
if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)
return BluetoothAdapter.getDefaultAdapter();
else {
BluetoothManager bm = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
return bm.getAdapter();
}
}
// Check whether Bluetooth tethering is enabled.
private boolean IsBluetoothTetherEnabled() {
try {
if(mBluetoothAdapter != null) {
return (Boolean) mIsBTTetheringOn.invoke(BTSrvInstance, (Object []) noparams);
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public class BTPanServiceListener implements BluetoothProfile.ServiceListener {
private final Context context;
public BTPanServiceListener(final Context context) {
this.context = context;
}
#Override
public void onServiceConnected(final int profile,
final BluetoothProfile proxy) {
//Some code must be here or the compiler will optimize away this callback.
Log.i("MyApp", "BTPan proxy connected");
}
#Override
public void onServiceDisconnected(final int profile) {
}
}