Android Open Source - android-sdk Mock Backend






From Project

Back to project page android-sdk.

License

The source code is released under:

MIT License

If you think the Android project android-sdk 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

package io.relayr.api;
//from  w  w w. j a  v  a 2 s. co m
import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import io.relayr.model.Reading;
import rx.Observable;
import rx.Subscriber;

public class MockBackend {

    public static final String AUTHORISE_USER = "authorise_user.json";
    public static final String USER_INFO = "user-info.json";
    public static final String APP_INFO = "app-info.json";
    public static final String USER_DEVICES = "user_devices.json";
    public static final String WEB_SOCKET_READINGS = "web_socket_reading.json";
    public static final String USERS_CREATE_WUNDERBAR = "users_create_wunderbar.json";
    public static final String USERS_TRANSMITTERS = "users_transmitters.json";
    public static final String USERS_TRANSMITTER = "users_transmitter.json";
    public static final String TRANSMITTER_DEVICES = "users_transmitters_devices.json";
    public static final String APPS_DEVICES_START = "apps_devices_start.json";
    public static final String PUBLIC_DEVICES = "public_devices.json";
    public static final String BOOKMARK_DEVICE = "bookmark_device.json";
    public static final String BOOKMARKED_DEVICES = "bookmarked_devices.json";
    public static final String DEVICE_MODELS = "device_models.json";
    public static final String DEVICE_READING_MEANINGS = "device_reading_meanings.json";
    public static final String SERVER_STATUS = "server-info.json";

    private final Context mContext;
    private Gson gson;

    @Inject
    public MockBackend(Context context) {
        mContext = context;

        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
        gson = gsonBuilder.create();
    }

    public String load(String fileName) throws Exception {
        StringBuilder fileContent = new StringBuilder();
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        try {
            inputStream = mContext.getAssets().open(fileName);
            inputStreamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = reader.readLine()) != null) {
                fileContent.append(line);
            }
        } finally {
            if (inputStreamReader != null)
                inputStreamReader.close();
            if (inputStream != null)
                inputStream.close();
            if (reader != null)
                reader.close();
        }
        return fileContent.toString();
    }

    public <T> T load(TypeToken<T> typeToken, String resource) throws Exception {
        return gson.fromJson(load(resource), typeToken.getType());
    }

    public Reading[] getWebSocketReadings() {
        try {
            List<Reading> readings = load(new TypeToken<List<Reading>>() {
            }, WEB_SOCKET_READINGS);
            return readings.toArray(new Reading[readings.size()]);
        } catch (Exception e) {
            return null;
        }
    }

    public <T> Observable<T> createObservable(final TypeToken<T> typeToken, final String asset) {
        return Observable.create(new Observable.OnSubscribe<T>() {
            @Override
            public void call(Subscriber<? super T> subscriber) {
                try {
                    subscriber.onNext(load(typeToken, asset));
                    subscriber.onCompleted();
                } catch (Exception e) {
                    subscriber.onError(e);
                }
            }
        });
    }

    class DateDeserializer implements JsonDeserializer<Date> {
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            Date date = null;
            try {
                date = sdf.parse(json.getAsJsonPrimitive().getAsString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return date;
        }
    }

}




Java Source Code List

io.relayr.DebugModules.java
io.relayr.LoginEventListener.java
io.relayr.Modules.java
io.relayr.RelayrApp.java
io.relayr.RelayrModule.java
io.relayr.RelayrSdk.java
io.relayr.SocketClient.java
io.relayr.activity.LoginActivity.java
io.relayr.activity.UiModule.java
io.relayr.api.ApiModule.java
io.relayr.api.CloudApi.java
io.relayr.api.DebugApiModule.java
io.relayr.api.MockBackend.java
io.relayr.api.MockCloudApi.java
io.relayr.api.MockOauthApi.java
io.relayr.api.MockRelayrApi.java
io.relayr.api.MockStatusApi.java
io.relayr.api.MockSubscriptionApi.java
io.relayr.api.OauthApi.java
io.relayr.api.RelayrApi.java
io.relayr.api.StatusApi.java
io.relayr.api.SubscriptionApi.java
io.relayr.api.Utils.java
io.relayr.api.package-info.java
io.relayr.ble.BleDeviceManager.java
io.relayr.ble.BleDeviceMode.java
io.relayr.ble.BleDeviceType.java
io.relayr.ble.BleDevice.java
io.relayr.ble.BleDevicesScanner.java
io.relayr.ble.BleModule.java
io.relayr.ble.BleScannerFilter.java
io.relayr.ble.BleSocketClient.java
io.relayr.ble.BleUtils.java
io.relayr.ble.BluetoothGattStatus.java
io.relayr.ble.DebugBleModule.java
io.relayr.ble.DeviceCompatibilityUtils.java
io.relayr.ble.MockBleUtils.java
io.relayr.ble.NullableRelayrBleSdk.java
io.relayr.ble.RelayrBleSdkImpl.java
io.relayr.ble.RelayrBleSdk.java
io.relayr.ble.parser.AdvertisementPacketParser.java
io.relayr.ble.parser.BleDataParser.java
io.relayr.ble.service.BaseService.java
io.relayr.ble.service.BluetoothGattReceiver.java
io.relayr.ble.service.BondingReceiver.java
io.relayr.ble.service.DirectConnectionService.java
io.relayr.ble.service.MasterModuleService.java
io.relayr.ble.service.OnBoardingService.java
io.relayr.ble.service.Service.java
io.relayr.ble.service.ShortUUID.java
io.relayr.ble.service.Utils.java
io.relayr.ble.service.error.CharacteristicNotFoundException.java
io.relayr.ble.service.error.DisconnectionException.java
io.relayr.ble.service.error.GattException.java
io.relayr.ble.service.error.ReadCharacteristicException.java
io.relayr.ble.service.error.WriteCharacteristicException.java
io.relayr.ble.service.package-info.java
io.relayr.ble.package-info.java
io.relayr.log.LoggerStorage.java
io.relayr.log.Logger.java
io.relayr.model.AccelGyroscope.java
io.relayr.model.App.java
io.relayr.model.BookmarkDevice.java
io.relayr.model.Bookmark.java
io.relayr.model.Command.java
io.relayr.model.CreateWunderBar.java
io.relayr.model.DeviceModel.java
io.relayr.model.Device.java
io.relayr.model.LightColorProx.java
io.relayr.model.LogEvent.java
io.relayr.model.ModelDefinition.java
io.relayr.model.Model.java
io.relayr.model.OauthToken.java
io.relayr.model.ReadingMeaning.java
io.relayr.model.Reading.java
io.relayr.model.Status.java
io.relayr.model.TransmitterDevice.java
io.relayr.model.Transmitter.java
io.relayr.model.User.java
io.relayr.model.WebSocketConfig.java
io.relayr.model.WunderBar.java
io.relayr.model.package-info.java
io.relayr.storage.DataStorage.java
io.relayr.storage.RelayrProperties.java
io.relayr.util.DebugUtilModule.java
io.relayr.util.MockReachabilityUtils.java
io.relayr.util.ReachabilityUtils.java
io.relayr.util.UtilModule.java
io.relayr.websocket.DebugWebSocketModule.java
io.relayr.websocket.MockWebSocketFactory.java
io.relayr.websocket.MockWebSocket.java
io.relayr.websocket.WebSocketCallback.java
io.relayr.websocket.WebSocketClient.java
io.relayr.websocket.WebSocketFactory.java
io.relayr.websocket.WebSocketModule.java
io.relayr.websocket.WebSocket.java
io.relayr.websocket.package-info.java
io.relayr.package-info.java