com.strongloop.android.loopback

## Class LocalInstallation

- java.lang.Object
- - com.strongloop.android.loopback.LocalInstallation

    public class LocalInstallation
    extends java.lang.Object

This class represents the Installation instance assigned to the installation of the Android application that is running this code. Certain properties are cached in `android.content.SharedPreferences` to prevent duplicate registrations.

    public class MyActivity extends Activity {

     // This method must be called from onCreate() method every time
     // the application starts. See the example Android application
     // in loopback-push-notification for more details.
     private void updateRegistration() {
         final Context context = getApplicationContext();

         // 1. Grab the shared RestAdapter instance.
         final DemoApplication app = (DemoApplication) getApplication();
         final RestAdapter adapter = app.getLoopBackAdapter();

         // 2. Create LocalInstallation instance
         final LocalInstallation installation =
                 new LocalInstallation(context, adapter);

         // 3. Update Installation properties that were not pre-filled

         // Enter the id of the application you registered in your LoopBack server
         // by calling Application.register()
         installation.setAppId(LOOPBACK_APP_ID);

         // Substitute a real id of the user logged in this application
         installation.setUserId("loopback-android");

         // 4. Check if we have a valid GCM registration id
         if (installation.getDeviceToken() != null) {
             // 5a. We have a valid GCM token, all we need to do now
             //     is to save the installation to the server
             saveInstallation(installation);
         } else {
             // 5b. We don't have a valid GCM token. Get one from GCM
             // and save the installation afterwards.
             registerInBackground(installation);
         }
     }

     private void registerInBackground(final LocalInstallation installation) {
         new AsyncTask<Void, Void, String>() {
             //Override
             protected String doInBackground(final Void... params) {
                 try {
                     final String regid = gcm.register(SENDER_ID);
                     installation.setDeviceToken(regid);
                     return "Device registered, registration ID=" + regid;
                 } catch (final IOException ex) {
                     Log.e(TAG, "GCM registration failed.", ex);
                     return "Cannot register with GCM:" + ex.getMessage();
                     // If there is an error, don't just keep trying to register.
                     // Require the user to click a button again, or perform
                     // exponential back-off.
                 }
             }

             //Override
             protected void onPostExecute(final String message) {
                 saveInstallation(installation);
                 // optionally log GCM registration result in `message`
             }
         }.execute(null, null, null);
     }

     void saveInstallation(final LocalInstallation installation) {
         installation.save(new Model.Callback() {
             //Override
             public void onSuccess() {
                 // Log success
             }
             //Override
             public void onError(final Throwable t) {
                 // Report error
             }
         });
     }
     }

### Field Summary

Fields
`static java.lang.String` `DEVICE_TYPE_ANDROID`
`static java.lang.String` `SHARED_PREFERENCES_NAME`
`static java.lang.String` `STATUS_ACTIVE`

### Constructor Summary

Constructors
Constructor and Description
`LocalInstallation(Context applicationContext, RestAdapter loopbackAdapter)` Creates a new instance of LocalInstallation class.

### Method Summary

Methods
`java.lang.String` `getAppId()`
`java.lang.String` `getAppVersion()`
`java.lang.String` `getDeviceToken()`
`java.lang.String` `getDeviceType()`
`java.lang.Object` `getId()`
`java.lang.String` `getStatus()`
`java.lang.String[]` `getSubscriptions()`
`java.lang.String` `getTimeZone()`
`java.lang.String` `getUserId()`
`void` `save(VoidCallback callback)` Saves the Installation to the remote server.
`void` `setAppId(java.lang.String appId)` See `getAppId()`.
`void` `setAppVersion(java.lang.String appVersion)` See `getAppVersion()`.
`void` `setDeviceToken(java.lang.String deviceToken)` See `getDeviceToken()`.
`void` `setStatus(java.lang.String status)` See `getStatus()`.
`void` `setSubscriptions(java.lang.String[] subscriptions)` See `getSubscriptions()`.
`void` `setTimeZone(java.lang.String timeZone)` See `getTimeZone()`
`void` `setUserId(java.lang.String userId)` See `getUserId()`.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### DEVICE_TYPE_ANDROID

    public static final java.lang.String DEVICE_TYPE_ANDROID

See Also:
  Constant Field Values

#### STATUS_ACTIVE

    public static final java.lang.String STATUS_ACTIVE

See Also:
  Constant Field Values

#### SHARED_PREFERENCES_NAME

    public static final java.lang.String SHARED_PREFERENCES_NAME

### Constructor Detail

#### LocalInstallation

    public LocalInstallation(Context applicationContext,
                     RestAdapter loopbackAdapter)

Creates a new instance of LocalInstallation class. Your application should never instantiate more than one instance.

Parameters:
  `applicationContext` - The Android context of your application.
  `loopbackAdapter` - The adapter to use for communication with the LoopBack server.

### Method Detail

#### getId

    public java.lang.Object getId()

Returns:
  The Installation id as assigned by the LoopBack server.

#### getAppId

    public java.lang.String getAppId()

Returns:
  The id of the LoopBack Application object this Installation is bound to.

#### setAppId

    public void setAppId(java.lang.String appId)

See `getAppId()`.

#### getAppVersion

    public java.lang.String getAppVersion()

Returns:
  The version of this Android application. The property is pre-filled for you using the value in the Android manifest.

#### setAppVersion

    public void setAppVersion(java.lang.String appVersion)

See `getAppVersion()`.

#### getUserId

    public java.lang.String getUserId()

Returns:
  Id of the user logged in the Android application.

#### setUserId

    public void setUserId(java.lang.String userId)

See `getUserId()`.

#### getDeviceType

    public java.lang.String getDeviceType()

Returns:
  One of the device types supported by LoopBack Push Notification plugin. This is a read-only property that returns `DEVICE_TYPE_ANDROID`

#### getDeviceToken

    public java.lang.String getDeviceToken()

Returns:
  The GCM registration id that can be used by the LoopBack server to push notifications to this application/device.

#### setDeviceToken

    public void setDeviceToken(java.lang.String deviceToken)

See `getDeviceToken()`.

#### getSubscriptions

    public java.lang.String[] getSubscriptions()

Returns:
  List subscriptions.

#### setSubscriptions

    public void setSubscriptions(java.lang.String[] subscriptions)

See `getSubscriptions()`.

#### getTimeZone

    public java.lang.String getTimeZone()

Returns:
  ID of the time zone preferred by the user. Example: America/Los_Angeles, Europe/London

#### setTimeZone

    public void setTimeZone(java.lang.String timeZone)

See `getTimeZone()`

#### getStatus

    public java.lang.String getStatus()

Returns:
  Status of this installation, the value is one of the strings recognized by LoopBack Push Notification plugin. See also `STATUS_ACTIVE`

#### setStatus

    public void setStatus(java.lang.String status)

See `getStatus()`.

#### save

    public void save(VoidCallback callback)

Saves the Installation to the remote server. When called for the first time, a new record is created and the id is stored in SharedPreferences. Subsequent calls updates the existing record with the stored id.

Parameters:
  `callback` - The callback to be executed when finished.