Hi all,

JavocSoft Android Toolbox Library

After a few weeks i have acumulated changes that fix a few things and improve a bit the library JavocSoft Android Toolbox. From now i will wait until i have enough changes before upgrading it. In this update there are changes related to location service, SQLite module and fixes to the ToolBox itsef. Below the list of changes:

  • ToolBox:
    • Lately I have been using notification button actions in some applications and  I realized that the method I had for it it wasn’t very good because I still had to code a bit. Because of this, I created a new method “notification_createActionButton()”. Now you only need to pass some arguments and do not need to code nothing to get the action. Remenber that notification buttons are available since Android 4.1+ (compatibility classes are used anyway). Now is really very easy to create notification action buttons 🙂
    • If you get in an application this message in the logcat:
      Choreographer(abc): Skipped xx frames! The application may be doing too much work on its main thread.
      You have a problem that need to be solved because it can be a perfomance problem in your app, you may be doing to much work in the main application UI thread.
      To help solving it, there is a new method “” that enables Android StrictMode warning logs and appropiate actions so you can locate the source code responsible of the bad perfomance.
    • There is also a new method “threadIsMainApplicationUIThread()” that returns TRUE if the current thread is the main application UI thread.
  • SQLite:
    • DBHelper. Added update methods “updateWhere()”.
    • SQLite. Fixed a bug in DBSQLite in the method “onUpgrade()”. The bug made updates to do not be done.
  • Location Service:
    • Now logs only are printed if the debug mode is ON.
    • Now location updates are discarded if is the same location or if the distance between location (appbox amazonapps using Haversine formula) is minor than the minimal distance threshold.

 

I hope this changes help you a bit more using JavocSoft Android Toolbox 🙂

Do not forget to check the blog to get more updates!

More info and HowTo at https://github.com/javocsoft/javocsoft-toolbox/wiki.

github_icon

As always, library is available on GitHub

javocsoft-toolbox.

Bye.

JavocSoft 2015.

Hi all again,

Today i have had time to isolate a class to have a localization service ready-to-use in the library JavocSoft Android Toolbox.

JavocSoft Android Toolbox LibraryThis will allow, very easily, to have an application that watches for any location change (or GPS enabled/disabled) event in background even if the application is not running because is an Android service 🙂

To use it, first declare in your AndroidManifest.xml:

<service android:name="es.javocsoft.android.lib.toolbox.location.service.LocationService" android:label="Location Service" android:enabled="true"/>

And launch it:

Intent mLocServiceIntent = new Intent(getBaseContext(), LocationService.class);
Bundle extras = new Bundle();
extras.putInt(LocationService.LOCATION_SERVICE_PARAM_MIN_DISTANCE, 2);
extras.putInt(LocationService.LOCATION_SERVICE_PARAM_MIN_TIME, 4000);
mLocServiceIntent.putExtras(extras);
startService(mLocServiceIntent);

And to stop it, when required:

Intent mLocServiceIntent = new Intent(getBaseContext(), LocationService.class);
stopService(mLocServiceIntent);

 

The localization service can also be customized when launched by setting in the service launching intent these parameters:

  • LocationService.LOCATION_SERVICE_PARAM_MIN_DISTANCE
  • LocationService.LOCATION_SERVICE_PARAM_MIN_TIME

Once is running, the service will broadcast the following events:

  • LOCATION_SERVICE_STARTED. Intent filter name:
    es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_SERVICE_STARTED
  • LOCATION_SERVICE_SHUTDOWN. Intent filter name:
    es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_SERVICE_SHUTDOWN
  • LOCATION_CHANGED. Intent filter name:
    es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_CHANGED
  • LOCATION_GPS_DISABLED. Intent filter name:
    es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_GPS_DISABLED
  • LOCATION_GPS_ENABLED. Intent filter name:
    es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_GPS_ENABLED

So now, to attend the localization events you only have to create a BroadcastReceiver:

public class LocationChangedReceiver extends WakefulBroadcastReceiver {
 
 protected static String TAG = "LocationChangedReceiver";
 
 
 public LocationChangedReceiver() {
 
 }
 @Override
 public void onReceive(Context context, Intent intent) {
 
   Bundle bundle = intent.getExtras(); 
   if(intent.getAction().equals(LocationService.ACTION_LOCATION_CHANGED)) { 
     //The Location Service leaves the detected location in the extras using
     //the key LocationService.LOCATION_KEY.
     Location location = bundle.getParcelable(LocationService.LOCATION_KEY);
     //Do something
   }else if(intent.getAction().equals(LocationService.ACTION_LOCATION_GPS_ENABLED)){
     //Do something
   }else if(intent.getAction().equals(LocationService.ACTION_LOCATION_GPS_DISABLED)){
     //Do something
   }else if(intent.getAction().equals(LocationService.ACTION_LOCATION_SERVICE_STARTED))
     //Do something 
   }else if(intent.getAction().equals(LocationService.ACTION_LOCATION_SERVICE_SHUTDOWN)){
     //Do something
   }
 }
}

And declare this receiver in your application AndroidManifest.xml:

<receiver android:name="your_application_package.LocationReceiver" android:enabled="true" android:exported="false"/>
 <intent-filter>
 <action android:name="es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_SERVICE_STARTED"/>
 <action android:name="es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_SERVICE_SHUTDOWN"/>
 <action android:name="es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_CHANGED"/>
 <action android:name="es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_GPS_ENABLED"/>
 <action android:name="es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_GPS_ENABLED"/>
 </intent-filter>
<receiver/>

 

I hope this addition make your life easier 🙂

Do not forget to check the blog to get more updates!

More info and HowTo at https://github.com/javocsoft/javocsoft-toolbox/wiki.

github_icon

As always, library is available on GitHub

javocsoft-toolbox.

Bye.

JavocSoft 2015.

Hi all,

With new versions of Android, the library needs to be adapted to deal with new possibilities JavocSoft Android Toolbox Libraryand requirements. In this case, the new library JavocSoft Android Toolbox version has resources to handle new Android 6+ Permission system. This will allow us to embrace and adapt our applications to Android 6 new permissions usage approach.

Android 6 and Permissions

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. If the device is running Android 5.1 (API level 22) or lower, or the app’s targetSdkVersion is 22 or lower, the system asks the user to grant the permissions at install time.

System permissions are divided into two categories, normal and dangerous (more info):

  • Normal permissions do not directly risk the user’s privacy. If your app lists a normal permission in its manifest, the system grants the permission automatically. See the list here.
  • Dangerous permissions can give the app access to the user’s confidential data. If your app lists a normal permission in its manifest, the system grants the permission automatically. If you list a dangerous permission, the user has to explicitly give approval to your app. If an app requests a dangerous permission listed in its manifest, and the app already has another dangerous permission in the same permission group, the system immediately grants the permission without any interaction with the user.

See Google Developer Permissions to know more about it.

Adapt your application

To adapt an application to the new permissions usage approach of Android 6+, the application has to be able to deal with these use cases:

  • Start the App. Ask for permission (showing before a message telling why do you need before ask for them)
    • Accept. Ask for permissions:
      • Accept -> App runs normally with the service that requires the permissions.
      • Not accept -> App runs normally without the service that requires the permissions.
    • Cancel. App runs normally without the service that requires the permissions.
  • With previously granted permissions, start the App:
    • Start the App -> App runs normally with the service that requires the permissions.
  • In a running App with granted permissions:
    • Deny the permissions -> When returning to the App, it Asks for permissions (showing before a message telling why do you need before ask for them):
      • Accept. Ask for permissions:
        • Accept -> App runs normally with the service that requires the permissions.
        • Not accept -> App runs normally without the service that requires the permissions.
      • Cancel. App runs normally without the service that requires the permissions.
  • User denies the permission and marks “Never ask again”
    • Start the App:
      • Show a message to the user about the required permissions (showing before a message telling why do you need before ask for them)
        • Accept. Ask for permissions:
          • Accept -> App runs normally with the service that requires the permissions.
          • Not accept -> App runs normally without the service that requires the permissions.
        • Cancel. App runs normally without the service that requires the permissions.

For devices with Android minor than 6 version, API Level 23, you should be able to open the application normally and use it without prompting for any permission because they are already granted when the application is installed.

 

The library JavocSoft Android Toolbox gives you the method to achieve these point in a relative easy way.

Usage

ToolBox provides methods to check for permissions allowing us to present to the user an informative dialog and also ask to allow them if they are not already granted. ToolBox uses the Android Support Library for backward Android compatibility.

The list of functions to handle permissions are:

  • permission_askFor
  • permission_checkAskPermissionsresult
  • permission_isGranted
  • permission_areGranted

ToolBox also provides a set of permissions packages according with Google permissions group. This makes easy for you to ask for permissions for a service that requires a set of permissions.

  • PERMISSION_CALENDAR
  • PERMISSION_CAMERA
  • PERMISSION_LOCATION
  • PERMISSION_MICROPHONE
  • PERMISSION_PHONE
  • PERMISSION_SENSORS
  • PERMISSION_SMS
  • PERMISSION_STORAGE

Here is an example of usage. In this case we are going to use the localization service.

This service requires two permissions that are not automatically granted (not in Google NORMAL permissions group):

  • ACCESS_COARSE_LOCATION
  • ACCESS_FINE_LOCATION

ToolBox has these two permissions in the PERMISSION_LOCATION set. We will use this set to ask for permission to the user:

<!-- Before use it, we check if the permissions are already granted. -->
if(!ToolBox.permission_areGranted(TheActivity.this, ToolBox.PERMISSION_LOCATION.keySet())) {
    //Permissions not yet granted, we need to be ask.
    ToolBox.permission_askFor(TheActivity.this, ToolBox.PERMISSION_LOCATION,     
            ToolBox.PERMISSIONS_REQUEST_CODE_ASK_LOCATION, 
        getString(R.string.permissions_required_title), 
        getString(R.string.permissions_button_ok),
        getString(R.string.permissions_button_deny),
        getString(R.string.permissions_location_required_text));
}else{
     //Permissions are already granted, continue using the localization service...
 
}

To handle the permissions ask response, we must do the following:

<!-- This method handles the response -->
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    permissionsLocationGranted = checkAskPermissionsresult(requestCode, permissions, grantResults);
    Log.i(Constants.TAG, "Location permissions granted? " + permissionsLocationGranted);
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
 
    if(permissionsLocationGranted) {
        //Continue with the usage of the service that needs the permissions...
 
    }else{
        //Do something if required...
    }        
}
 
private boolean checkAskPermissionsresult(int requestCode, String[] permissions, int[] grantResults) {
    boolean res = false;   
    if(requestCode == ToolBox.PERMISSIONS_REQUEST_CODE_ASK_LOCATION) {
            //We could check the permissions in our system
        //res = ToolBox.permission_areGranted(SplashActivity.this, Arrays.asList(permissions));         
            //We check the returned result. Only returns TRUE if all permissions are granted.
        res = ToolBox.permission_checkAskPermissionsresult(permissions, grantResults);
    }
    return res;
}

Have in mind that every time you use a service o function that requires permissions, you should check the current permissions by using:

ToolBox.permission_areGranted(TheActivity.this, ToolBox.PERMISSION_LOCATION.keySet());

To know more about it goto https://github.com/javocsoft/javocsoft-toolbox/wiki#android-6-permissions-handle.

 

Recommended videos about permissions in Android M that you should check:

 

I hope these addition could be useful for your applications.

Do not forget to check the blog to get more updates!

More info and HowTo at https://github.com/javocsoft/javocsoft-toolbox/wiki.

github_icon

As always, library is available on GitHub

javocsoft-toolbox.

Bye.

JavocSoft 2015.

Hi,

In this update I added some assistance classes related with inter-application messaging in Android to JavocSoft Android Toolbox.JavocSoft Android Toolbox Library

Sometimes an application needs to accept messages from other applications, in those cases, a Messenger service must be implemented. See Messenger for more information about messaging. So, to make easier having a messenger service in an application, i have added a base MessengerService.java class to the library. Click here to access to the Messenger Service Wiki.

Here can be seen full example of how to have a valid Messenger Service in your application:

First, create the message incoming handler:

public class MyMessengerIncomingHandler extends MessengerIncomingHandler {

    //Messenger WHAT possible values
    public static final int MSG_EVT_HI = 1000;

    public MyMessengerIncomingHandler(Context context) {
        super(context);
    }

    @Override
    protected void doWork(Message msg) {
        switch (msg.what) {
            case MSG_EVT_HI:
                Log.i(Constants.TAG, "Messenger received: Hi!!");
                break;
            default:
                super.handleMessage(msg);
        }
    }
}

Second, the Messenger Service:

 public class MyMessengerService extends MessengerService {

 @Override
 protected MessengerIncomingHandler getMessageIncomingHandler() {
    return new MyMessengerIncomingHandler(context);
 }

}

And finally, in our AndroidManifest.xml:

<!-- Application messenger -->
<service android:name="es.javocsoft.base.genapp.service.MyMessengerService" 
	 android:enabled="true" android:exported="true" >
   <intent-filter>             
	<action android:name="es.javocsoft.base.genapp.messenger.ACTION_BIND" />
   </intent-filter>
</service>

Because we could need also to connect and send messages to other applications messenger services, I also added a class Mezzenger.java. Click here to access to the Messenger Wiki.

 

Mezzenger messenger = new Mezzenger("Inner", getApplicationContext()); 
messenger.connect("application.package.service.ACTION"); 
messenger.sendMessage(MSG_EVT_HI, 0, 0, null);

 

Do not forget to check the blog to get more updates!

More info and HowTo at https://github.com/javocsoft/javocsoft-toolbox/wiki.

github_icon

As always, library is available on GitHub

javocsoft-toolbox.

Bye.

JavocSoft 2015.

Hi all,

Today a new version of the library JavocSoft Android Toolbox have been released, in this case with a new module that integrates the Google Drive API. This allows to use Android ToolBox - Google DriveGoogle Drive within your applications in a really easy way. I wanted to add this to the library long time ago but until a few weeks i could not start doing it.JavocSoft Android Toolbox Library

To know how to use it and more  goto https://github.com/javocsoft/javocsoft-toolbox/wiki#google-drive.

As a brief, the list of operations that the new module for Google Drive allows are:

  • Create a file (with the posibility of creating it in the secure application folder).
  • Search for files.
  • Get a file given its name or title
  • Get the file contents.
  • Pin a file (this makes Google Drive to store a copy of its metadata locally in the Device).
  • Trash/Un-trash a file.
  • Be notified when a file changes (see TBDrive#addFileChangeListener() and TBChangeListener.java)
  • Subscribe to file changes (notifications are received even if the application is not running. See TBDrive#subscribeToFile() and TBDriveEventService.java).
  • Commit/Discard changes to a file (also be notified when Google Drive servers finish applying changes, see TBDriveEventService.java)
  • Get file contents as an String.
  • Append/Replace text to a file.
  • Get a DriveFile object from an encoded DriveId string.

As always, I hope these addition could be useful for your applications.

Do not forget to check the blog to get more updates!

More info and HowTo at https://github.com/javocsoft/javocsoft-toolbox/wiki.

github_icon

As always, library is available on GitHub

javocsoft-toolbox.

Bye.

JavocSoft 2015.

Google IO 2015 – GCM 3.0

Google I/O 2015

Para los que aún no hayan visto aún esta edición, la 2015, entre las novedades, referente a GCM (Google Clud Messaging), en su versión 3.0, hay una serie de novedades que merecen ser nombradas:

Para no perderte las novedades, si aún no lo viste, haz clic en Google I/O 2015 – Google Cloud Messagin 3.0. Tenéis también la web https://developers.google.com/cloud-messaging en donde poder ver todo lo relativo a GCM.

Como siempre, poco a poco iré integrando estas novedades en la librería Android javocsoft-toolbox, para que podáis disfrutar de dichos cambios de una manera sencilla.

 

Os dejo los videos, a modo recopilatorio, del evento ya pasado.

Y aquí otras exposiciones sobre temas concretos:

 

Un saludo,