Android Open Source - android-levelup Gate Storage






From Project

Back to project page android-levelup.

License

The source code is released under:

MIT License

If you think the Android project android-levelup 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) 2012-2014 Soomla Inc.//from   w  w w . j a  v  a 2s  .c  o  m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.soomla.levelup.data;

import android.text.TextUtils;

import com.soomla.BusProvider;
import com.soomla.data.KeyValueStorage;
import com.soomla.levelup.LevelUp;
import com.soomla.levelup.events.GateClosedEvent;
import com.soomla.levelup.events.GateOpenedEvent;

/**
 * A utility class for persisting and querying the state of gates.
 * Use this class to check if a certain gate is open, or to open it.
 * This class uses the <code>KeyValueStorage</code> internally for storage.
 * <p/>
 * Created by refaelos on 13/05/14.
 */
public class GateStorage {

    private static String keyGates(String gateId, String postfix) {
        return DB_GATE_KEY_PREFIX + gateId + "." + postfix;
    }

    private static String keyGateOpen(String gateId) {
        return keyGates(gateId, "open");
    }

    /**
     * Opens or closes the given gate.
     *
     * @param gateId the id of the gate to change status
     * @param open the status (<code>true</code> for open,
     *             <code>false</code> for closed)
     */
    public static void setOpen(String gateId, boolean open) {
        setOpen(gateId, open, true);
    }

    public static void setOpen(String gateId, boolean open, boolean notify) {
        String key = keyGateOpen(gateId);

        if (open) {
            KeyValueStorage.setValue(key, "yes");

            if (notify) {
                BusProvider.getInstance().post(new GateOpenedEvent(gateId));
            }
        } else {
            KeyValueStorage.deleteKeyValue(key);

            if (notify) {
                BusProvider.getInstance().post(new GateClosedEvent(gateId));
            }
        }
    }

    /**
     * Checks if the given gate is open.
     *
     * @param gateId the id of the gate to check
     * @return <code>true</code> if open, <code>false</code> otherwise
     */
    public static boolean isOpen(String gateId) {
        String key = keyGateOpen(gateId);
        String val = KeyValueStorage.getValue(key);
        return !TextUtils.isEmpty(val);
    }

    public static final String DB_GATE_KEY_PREFIX = LevelUp.DB_KEY_PREFIX + "gates.";
}




Java Source Code List

com.soomla.levelup.LevelUp.java
com.soomla.levelup.data.GateStorage.java
com.soomla.levelup.data.LUJSONConsts.java
com.soomla.levelup.data.LevelStorage.java
com.soomla.levelup.data.MissionStorage.java
com.soomla.levelup.data.ScoreStorage.java
com.soomla.levelup.data.WorldStorage.java
com.soomla.levelup.events.GateClosedEvent.java
com.soomla.levelup.events.GateOpenedEvent.java
com.soomla.levelup.events.LatestScoreChangedEvent.java
com.soomla.levelup.events.LevelEndedEvent.java
com.soomla.levelup.events.LevelStartedEvent.java
com.soomla.levelup.events.LevelUpInitializedEvent.java
com.soomla.levelup.events.MissionCompletedEvent.java
com.soomla.levelup.events.MissionCompletionRevokedEvent.java
com.soomla.levelup.events.ScoreRecordChangedEvent.java
com.soomla.levelup.events.ScoreRecordReachedEvent.java
com.soomla.levelup.events.WorldAssignedRewardEvent.java
com.soomla.levelup.events.WorldCompletedEvent.java