Android Open Source - blekit-android Beacon Preferences






From Project

Back to project page blekit-android.

License

The source code is released under:

Copyright (C) 2014 Upnext Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software wi...

If you think the Android project blekit-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2014 UP-NEXT. All rights reserved.
 * http://www.up-next.com/*w w  w  .ja  v a  2 s.  co  m*/
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package com.upnext.blekit.util;

import android.content.Context;

import com.sentaca.dbpreferences.DatabaseBasedSharedPreferences;
import com.upnext.blekit.BLEKitClient;
import com.upnext.blekit.Proximity;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Persistence layer for beacons data.
 *
 * @author Roman Wozniak (roman@up-next.com)
 */
public class BeaconPreferences {

    private static DatabaseBasedSharedPreferences mPrefs;

    private static final String LAST_ZONE_JSON = "last_zone_json";
    private static final String TARGET_ACTIVITY_FOR_NOTIFICATIONS = "target_activity_for_notifications";

    private static final String RUNNING_CLIENTS = "running_clients";
    private static final String SEPARATOR_CLIENT_VALUES = "#";
    private static final String SEPARATOR_BEACON_VALUES = ",";

    private static final String MONITORED_BEACONS = "monitored_beacons";

    private static DatabaseBasedSharedPreferences getPrefs(Context context) {
        if(mPrefs==null) {
            mPrefs = new DatabaseBasedSharedPreferences(context);
        }
        return mPrefs;
    }

    public static Set<BLEKitClient> getRunningClients( Context context ) {
        Set<String> clients = getPrefs(context).getStringSet(RUNNING_CLIENTS);
        Set<BLEKitClient> result = new HashSet<BLEKitClient>();
        if( clients!=null ) {
            for( String clientString : clients ) {
                String[] vals = clientString.split(SEPARATOR_CLIENT_VALUES);

                String pkg = vals[0];
                String inBg = vals[1];
                String beacons = vals[2];

                BLEKitClient c = new BLEKitClient(pkg, Boolean.valueOf(inBg), beaconsFromString(beacons));
                result.add(c);
            }
        }
        return result;
    }

    private static Set<String> beaconsFromString(String beacons) {
        Set<String> result = new HashSet<String>();
        if(beacons!=null) {
            Collections.addAll(result, beacons.split(SEPARATOR_BEACON_VALUES));
        }
        return result;
    }

    private static String beaconsToString(Set<String> beacons) {
        StringBuffer buf = new StringBuffer();
        for( String beacon : beacons ) {
            buf.append(beacon).append(SEPARATOR_BEACON_VALUES);
        }
        if( buf.length()>0 ) {
            buf.deleteCharAt(buf.length()-1);
        }
        return buf.toString();
    }

    public static void setRunningClients( Context context, Set<BLEKitClient> clients ) {
        Set<String> clientsSet = new HashSet<String>();
        for( BLEKitClient client : clients ) {
            clientsSet.add(
                    client.getPackageName() + SEPARATOR_CLIENT_VALUES +
                            client.isInBackground() + SEPARATOR_CLIENT_VALUES +
                            beaconsToString(client.getMonitoredBeaconIDs())
            );
        }

        getPrefs(context).putStringSet(RUNNING_CLIENTS, clientsSet );
    }

    public static void addClient( Context context, BLEKitClient clientToAdd ) {
        Set<BLEKitClient> current = getRunningClients(context);
        if( current==null ) {
            current = new HashSet<BLEKitClient>();
        }

        boolean pkgFound = false;
        for( BLEKitClient client : current ) {
            if( client.getPackageName().equals(clientToAdd.getPackageName()) ) {
                pkgFound = true;
            }
        }

        if( !pkgFound ) {
            current.add(clientToAdd);
            setRunningClients(context, current);
        }
    }

    public static void removeClient( Context context, String clientPackage ) {
        Set<BLEKitClient> current = getRunningClients(context);
        if( current==null ) {
            return;
        }

        for( BLEKitClient client : current ) {
            if( client.getPackageName().equals(clientPackage) ) {
                current.remove(client);
                setRunningClients(context, current);
                return;
            }
        }
    }

    public static String getLastZoneJson( Context context ) {
        return getPrefs(context).getString(LAST_ZONE_JSON, null);
    }

    public static void setLastZoneJson( Context context, String json ) {
        getPrefs(context).putString(LAST_ZONE_JSON, json);
    }

    public static String getTargetActivityForNotifications( Context context ) {
        return getPrefs(context).getString(TARGET_ACTIVITY_FOR_NOTIFICATIONS, null);
    }

    public static void setTargetActivityForNotifications( Context context, String activity ) {
        getPrefs(context).putString(TARGET_ACTIVITY_FOR_NOTIFICATIONS, activity);
    }

    public static Map<String, Proximity> getMonitoredBeacons(Context context) {
        final Set<String> values = getPrefs(context).getStringSet(MONITORED_BEACONS);
        final Map<String, Proximity> result = new HashMap<String, Proximity>();
        if( values!=null ) {
            for( String value : values ) {
                final String[] split = value.split(SEPARATOR_BEACON_VALUES);
                final String beaconId = split[0];
                final Proximity proximity = Proximity.valueOf(split[1]);
                result.put(beaconId, proximity);
            }
        }
        return result;
    }

    public static void setMonitoredBeacons(Context context, Map<String, Proximity> monitoredBeacons) {
        if( monitoredBeacons==null || monitoredBeacons.isEmpty() ) return;
        final Set<String> values = new HashSet<String>();
        for( String beacondId : monitoredBeacons.keySet() ) {
            values.add( beacondId + SEPARATOR_BEACON_VALUES + monitoredBeacons.get(beacondId).name() );
        }
        getPrefs(context).putStringSet(MONITORED_BEACONS, values);
    }
}




Java Source Code List

com.upnext.blekit.ActionsFactory.java
com.upnext.blekit.AverageIBeacon.java
com.upnext.blekit.BLEKitClient.java
com.upnext.blekit.BLEKitIntentProcessor.java
com.upnext.blekit.BLEKitService.java
com.upnext.blekit.BLEKit.java
com.upnext.blekit.BackgroundMode.java
com.upnext.blekit.BeaconEvent.java
com.upnext.blekit.ConditionsFactory.java
com.upnext.blekit.Config.java
com.upnext.blekit.EventOccurenceUnit.java
com.upnext.blekit.Proximity.java
com.upnext.blekit.StartupReceiver.java
com.upnext.blekit.actions.AlertActionParams.java
com.upnext.blekit.actions.AlertAction.java
com.upnext.blekit.actions.BLEAction.java
com.upnext.blekit.actions.BaseNotificationParams.java
com.upnext.blekit.actions.ContentActionParams.java
com.upnext.blekit.actions.ContentAction.java
com.upnext.blekit.actions.NotificationActionParams.java
com.upnext.blekit.actions.NotificationAction.java
com.upnext.blekit.actions.YelpActionParams.java
com.upnext.blekit.actions.YelpAction.java
com.upnext.blekit.actions.facebook.FacebookCheckinActionParams.java
com.upnext.blekit.actions.facebook.FacebookCheckinAction.java
com.upnext.blekit.actions.facebook.FacebookCheckinActivity.java
com.upnext.blekit.actions.foursquare.FoursquareCheckinActionParams.java
com.upnext.blekit.actions.foursquare.FoursquareCheckinAction.java
com.upnext.blekit.actions.foursquare.FoursquareCheckinActivity.java
com.upnext.blekit.conditions.BLECondition.java
com.upnext.blekit.conditions.CameFarCondition.java
com.upnext.blekit.conditions.CameImmediateCondition.java
com.upnext.blekit.conditions.CameNearCondition.java
com.upnext.blekit.conditions.EnterCondition.java
com.upnext.blekit.conditions.HttpOkCondition.java
com.upnext.blekit.conditions.HttpOkParams.java
com.upnext.blekit.conditions.LeaveCondition.java
com.upnext.blekit.conditions.OccurenceCondition.java
com.upnext.blekit.conditions.OccurenceParams.java
com.upnext.blekit.conditions.StaysCondition.java
com.upnext.blekit.conditions.StaysParams.java
com.upnext.blekit.listeners.BLEKitStateListener.java
com.upnext.blekit.listeners.BeaconEventListener.java
com.upnext.blekit.listeners.ZoneUpdateListener.java
com.upnext.blekit.model.Action.java
com.upnext.blekit.model.Beacon.java
com.upnext.blekit.model.Condition.java
com.upnext.blekit.model.CurrentBeaconProximity.java
com.upnext.blekit.model.Location.java
com.upnext.blekit.model.Trigger.java
com.upnext.blekit.model.Zone.java
com.upnext.blekit.receiver.LoggingReceiver.java
com.upnext.blekit.util.BeaconPreferences.java
com.upnext.blekit.util.BeaconsDB.java
com.upnext.blekit.util.ExpressionEvaluator.java
com.upnext.blekit.util.JsonParser.java
com.upnext.blekit.util.L.java
com.upnext.blekit.util.Rand.java
com.upnext.blekit.util.http.Error.java
com.upnext.blekit.util.http.HttpClient.java
com.upnext.blekit.util.http.HttpUtils.java
com.upnext.blekit.util.http.Response.java