Android Example Various Utilities created with Inline Java Code

Johan Schoeman

Expert
Licensed User
The attached project (VariousUtilities.zip) has a large number of Bitmap and String Utilities. There is also a utility that lists the sensors of your device in the Log. Three B4A classes are defined:
1. StringUtilities
2. BitmapUtilities
3. SensorUtilities

All the methods in the classes are based on inline java code with B4A code inside the classes serving as the interface to the java code.

Results of all methods in classes StringUtilities and SensorUtilities are reported in the Log of the project. For BitmapUtilities - click on the left imageview to cycle through the various Bitmap methods. You can pass different parameters to most of the methods in class BitmapUtilities to change the appearance of an image that is generated with a specific method.

You can add to the project (complimenting/adding to the existing classes or adding new classes with their own methods supporting the new class - eg DateUtilities, TimeUtilities, etc) and compile it to a library. The project as it is at present was compiled to a library (VariousUtils.zip) and the library files (jar and xml) are included in the /files folder of the project.

Please note that when you open the B4A project and go to the Bitmap class you might find an error on line 1893. For some reason the "return" statement keeps on reverting to "Return" when the project is opened. Can't figure out why it does so. Just change the "Return" to "return" and the project will compile.

All of the Java code were found somewhere on the Web - mostly from StackOverflow
 

Attachments

Johan Schoeman

Expert
Licensed User
Have added 20 odd Date/Time Utilities to the project making use of inline java code - found mostly on StackOverflow. It prints the results of the methods in the Date/Time Utilities to the Log. Have also compiled it to a library. Library files (jar and xml) are in the /files folder of the project (although you don't need them as the code is in the classes). Change the naming of the methods so that it perhaps makes better sense to you (in Java Code, Java B4A interface, and in main activity).
 

Attachments

Johan Schoeman

Expert
Licensed User
It adds class VibrateUtilities. It will vibrate once if you click on the left imageview (you can set/change the vibration time in Sub Globals ---> Dim duration As Long = 200). Added 4 buttons:
1. Vibrate a pattern once
2. Vibrate once for a length of time specified by "duration"
3. Vibrate a pattern repeatedly
4. Cancel the vibration activated by (3) above
 

Attachments

Johan Schoeman

Expert
Licensed User
It adds class BatteryUtilities. It prints to the Log and reports about various states/conditions of the battery. The value reported by EXTRA_TEMPERATURE must be divided by 10 and that of EXTRA_VOLTAGE by 1000. They are indexes 6 and 7 of the returned integer array.
 

Attachments

Johan Schoeman

Expert
Licensed User
It adds class TorchUtilities. It switches the camera flash into torch mode on/off. Not sure if this will work on all types of devices. Use the last two buttons from the bottom to switch the torch on/off
 

Attachments

Johan Schoeman

Expert
Licensed User
Added class ContactUtilities that extracts 17 fields of info from each contact on your device and prints them to the Log. Seem to extract the contacts on your Google account as well.....? Probably not the fastest way to do it but it does return the requested info.

Please feel free to add to this project any other utilities created from inline java code that might be useful - and then please post it in this thread.
 

Attachments

Last edited:

Johan Schoeman

Expert
Licensed User
Added class InstalledUtilities that will list all installed components on your device in the log (returned in the form of a List from the class containing the inline Java code)
 

Attachments

cimperia

Active Member
Licensed User
Will you ever stop? ;)

Thank you for sharing.
 

Johan Schoeman

Expert
Licensed User
Have added some more inline java code utilities:
MathUtilities - BubbleSortDecending, BubbleSortAscending, isPrime, sortArrayOfDoublesFromSmallToLarge, sortArrayOfDoublesFromLargeToSmall, ConvertIntToHex, ConvertIntToOct, ConvertIntToBin, getFactorial, locGreatCircleNm, hsGreatCircleNm, hsGreatCircleKm

...GreatCircle... calculates the great circle distance via law-of-cosines (loc) and via the Haversine Formula (nautical miles and kilometers)

NetworkUtilities - getConnectivityStatusString, isOnline, checkWifiMobileStatus, getWifiName, getMobileNetworkName, getGateway

isOnline could be useful for prepaid. ".....I found it very useful on devices that use prepaid cards. When they run out of money, they have internet access available, but no internet..."

TelephonyUtilities - getTelephonyInfo

All of the above methods in the 3 x additional classes reporting results to the log.
 

Attachments

Last edited:

Johan Schoeman

Expert
Licensed User
Added listPackages to class InstalledUtilities. It lists all app names, package names, version names, and version codes of apps on your device to the log. Also added DisplayUtilities that reports to the log.

All code found somewhere on Stackoverflow.
 

Attachments

Johan Schoeman

Expert
Licensed User
Added class InfoUtilities that returns an array of Strings with LOTS of info about:
BUILD Information
System Properties
Memory Info
Display Info
SystemAvailableFeatures
SystemSharedLibraryNames

to the B4A project - and then prints it to the log.
 

Attachments

stari

Active Member
Licensed User
ops, in this new version i get error, i thing is asocciated with InfoUtilities.
upload_2015-5-24_14-25-48.png
 

Johan Schoeman

Expert
Licensed User
ops, in this new version i get error, i thing is asocciated with InfoUtilities.
Comment out the part in InfoUtilities that reports on the memory and then see if it returns the remainder of the information. Comment it out like this:

B4X:
//  cntr = cntr + 1;
//  mInfo[cntr] = " ";
//  strInfo += "\n - Memory Info - \n";
//  cntr = cntr + 1;
//  mInfo[cntr] =  " - Memory Info - ";
//  MemoryInfo memoryInfo = new MemoryInfo();
//  ActivityManager activityManager = (ActivityManager)BA.applicationContext.getSystemService(Context.ACTIVITY_SERVICE);
//  activityManager.getMemoryInfo(memoryInfo);
//  strInfo += "Total Memory : " + memoryInfo.totalMem + "\n"; //API Level 16
//  cntr = cntr + 1;
//  mInfo[cntr] = "Total Memory : " + memoryInfo.totalMem;
//  strInfo += "Available Memory : " + memoryInfo.availMem + "\n";
//  cntr = cntr + 1;
//  mInfo[cntr] = "Available Memory : " + memoryInfo.availMem;
//  strInfo += "Threshold : " + memoryInfo.threshold + "\n";
//  cntr = cntr + 1;
//  mInfo[cntr] = "Threshold : " + memoryInfo.threshold;
//  strInfo += "Low Memory : " + memoryInfo.lowMemory + "\n";
//  cntr = cntr + 1;
//  mInfo[cntr] = "Low Memory : " + memoryInfo.lowMemory;
Code runs OK on my S4 mini with Android 4.2.2 but getting the same error on my tablet with Android 4.0.4. The "memory" part of the code only works with API Level >= 16 (Andoid 4.1)
 

Johan Schoeman

Expert
Licensed User
If you replace the (blocked) code in post #17 with this:

B4X:
  cntr = cntr + 1;
  mInfo[cntr] = " ";
  strInfo += "\n - Memory Info - \n";
  cntr = cntr + 1;
  mInfo[cntr] =  " - Memory Info - ";
  MemoryInfo memoryInfo = new MemoryInfo();
  ActivityManager activityManager = (ActivityManager)BA.applicationContext.getSystemService(Context.ACTIVITY_SERVICE);
  activityManager.getMemoryInfo(memoryInfo);
//  strInfo += "Total Memory : " + memoryInfo.totalMem + "\n"; API Level 16
  cntr = cntr + 1;
//  mInfo[cntr] = "Total Memory : " + memoryInfo.totalMem;
  mInfo[cntr] = "";
  strInfo += "Available Memory : " + memoryInfo.availMem + "\n";
  cntr = cntr + 1;
  mInfo[cntr] = "Available Memory : " + memoryInfo.availMem;
  strInfo += "Threshold : " + memoryInfo.threshold + "\n";
  cntr = cntr + 1;
  mInfo[cntr] = "Threshold : " + memoryInfo.threshold;
  strInfo += "Low Memory : " + memoryInfo.lowMemory + "\n";
  cntr = cntr + 1;
  mInfo[cntr] = "Low Memory : " + memoryInfo.lowMemory;
then it should report on all 3 the other parameters (i.e Available Memory, Threshold, and Low Memory). Need to figure out the Total Memory part when time allows.
 
Last edited:

stari

Active Member
Licensed User
OK, everything works fine at all Android-s. ops, not tested on Froyo, because I do not have any. However, it does not matter.
Thks, Johan .
 
Top