<commit_before>package cz.vutbr.fit.iha.controller;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import cz.vutbr.fit.iha.Constants;
import cz.vutbr.fit.iha.activity.LoginActivity;
import cz.vutbr.fit.iha.adapter.Adapter;
import cz.vutbr.fit.iha.adapter.device.Device;
import cz.vutbr.fit.iha.adapter.device.Device.SaveDevice;
import cz.vutbr.fit.iha.adapter.device.DeviceLog;
import cz.vutbr.fit.iha.adapter.device.DeviceLog.DataInterval;
import cz.vutbr.fit.iha.adapter.device.DeviceLog.DataType;
import cz.vutbr.fit.iha.adapter.device.DeviceType;
import cz.vutbr.fit.iha.adapter.device.Facility;
import cz.vutbr.fit.iha.adapter.location.Location;
import cz.vutbr.fit.iha.exception.NotImplementedException;
import cz.vutbr.fit.iha.household.ActualUser;
import cz.vutbr.fit.iha.household.Household;
import cz.vutbr.fit.iha.household.User;
import cz.vutbr.fit.iha.network.DemoNetwork;
import cz.vutbr.fit.iha.network.GoogleAuth;
import cz.vutbr.fit.iha.network.INetwork;
import cz.vutbr.fit.iha.network.Network;
import cz.vutbr.fit.iha.network.exception.FalseException;
import cz.vutbr.fit.iha.network.exception.NetworkException;
import cz.vutbr.fit.iha.pair.LogDataPair;
import cz.vutbr.fit.iha.persistence.Persistence;
import cz.vutbr.fit.iha.util.Log;
import cz.vutbr.fit.iha.util.Utils;

/**
 * Core of application (used as singleton), provides methods and access to all data and household.
 * 
 * @author Robyer
 */
public final class Controller {

	public static final String TAG = Controller.class.getSimpleName();

	/** This singleton instance. */
	private static Controller mController;

	/** Application context */
	private final Context mContext;

	/** Persistence service for caching purposes */
	private final Persistence mPersistence;

	/** Network service for communication with server */
	private final INetwork mNetwork;

	/** Household object holds logged in user and all adapters and lists which belongs to him */
	private final Household mHousehold;

	/** Switch for using demo mode (with example adapter, without server) */
	private static boolean mDemoMode = false;

	/**
	 * Return singleton instance of this Controller. This is thread-safe.
	 * 
	 * @param context
	 *            This must be the global application context.
	 * @return singleton instance of controller
	 */
	public static Controller getInstance(Context context) {
		if (mController == null) {
			synchronized (Controller.class) {
				if (mController == null) {
					mController = new Controller(context);
				}
			}
		}

		return mController;
	}

	/**
	 * Private constructor.
	 * 
	 * @param context
	 *            This must be the global application context.
	 */
	private Controller(Context context) {
		mContext = context;

		mNetwork = mDemoMode ? new DemoNetwork(context) : new Network(mContext, this, "userID", Utils.isDebugVersion(context)); //FIXME: ROB: if you have user id, give me, else call put something and call getUID method
		mPersistence = new Persistence(mContext);
		mHousehold = new Household(mContext, mNetwork);
		mNetwork.setUser(mHousehold.user);
	}

	/**
	 * @param context
	 *            This must be the global Application context.
	 * @param demoMode
	 */
	public static synchronized void setDemoMode(Context context, boolean demoMode) {
		if (mDemoMode == demoMode)
			return;

		mDemoMode = demoMode;
		mController = new Controller(context);

		if (demoMode) {
			// Initialize default settings for demo mode, because in demo mode we don't call login()
			mController.mPersistence.initializeDefaultSettings(mController.mHousehold.user.getEmail());
		}
	}

	public static boolean isDemoMode() {
		return mDemoMode;
	}

	/** Persistence methods *************************************************/

	public String getLastEmail() {
		return mPersistence.loadLastEmail();
	}

	/**
	 * Get SharedPreferences for actually logged in user
	 * 
	 * @return null if user is not logged in
	 */
	public SharedPreferences getUserSettings() {
		String userEmail = mHousehold.user.getEmail();
		if (userEmail == null || userEmail.isEmpty()) {
			return null;
		}

		return mPersistence.getSettings(userEmail);
	}

	/** Communication methods ***********************************************/

	/**
	 * Login user by his email (authenticate on server).
	 * 
	 * @param email
	 * @return true on success, false otherwise
	 * @throws NetworkException
	 */
	public boolean login(String email) throws NetworkException {
		// TODO: catch and throw proper exception
		// FIXME: after some time there should be picture in ActualUser object, should save to mPersistence
		try {
			
			if (((Network)mNetwork).getUID(email) || mNetwork.signIn(email, getGCMRegistrationId())) { // FIXME: gcmid have to be set separate now!!!, and here use getUID if you dont have userID
				mPersistence.saveLastEmail(email);
				mPersistence.initializeDefaultSettings(email);
				return true;
			}
		} catch (FalseException e) {
			// FIXME: ROB, do this how you want, this is working code :)
			switch (e.getDetail().getErrCode()) {
			case 0:
				break;
			case 2: //FIXME: there are constants of error in Constants.java file
			case 3: // bad token or email
				try {
					// TODO: do this otherway
					// GoogleAuth ggAuth = GoogleAuth.getGoogleAuth();
					//
					// ggAuth.invalidateToken();
					// ggAuth.doInForeground((ggAuth.getPictureIMG() == null)? true:false);

					if (mNetwork instanceof Network) {
						((Network) mNetwork).startGoogleAuth(true, getActualUser().isPictureDefault() ? true : false);<commit_msg>Fix demo mode<commit_after>package cz.vutbr.fit.iha.controller;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import cz.vutbr.fit.iha.Constants;
import cz.vutbr.fit.iha.activity.LoginActivity;
import cz.vutbr.fit.iha.adapter.Adapter;
import cz.vutbr.fit.iha.adapter.device.Device;
import cz.vutbr.fit.iha.adapter.device.Device.SaveDevice;
import cz.vutbr.fit.iha.adapter.device.DeviceLog;
import cz.vutbr.fit.iha.adapter.device.DeviceLog.DataInterval;
import cz.vutbr.fit.iha.adapter.device.DeviceLog.DataType;
import cz.vutbr.fit.iha.adapter.device.DeviceType;
import cz.vutbr.fit.iha.adapter.device.Facility;
import cz.vutbr.fit.iha.adapter.location.Location;
import cz.vutbr.fit.iha.exception.NotImplementedException;
import cz.vutbr.fit.iha.household.ActualUser;
import cz.vutbr.fit.iha.household.Household;
import cz.vutbr.fit.iha.household.User;
import cz.vutbr.fit.iha.network.DemoNetwork;
import cz.vutbr.fit.iha.network.GoogleAuth;
import cz.vutbr.fit.iha.network.INetwork;
import cz.vutbr.fit.iha.network.Network;
import cz.vutbr.fit.iha.network.exception.FalseException;
import cz.vutbr.fit.iha.network.exception.NetworkException;
import cz.vutbr.fit.iha.pair.LogDataPair;
import cz.vutbr.fit.iha.persistence.Persistence;
import cz.vutbr.fit.iha.util.Log;
import cz.vutbr.fit.iha.util.Utils;

/**
 * Core of application (used as singleton), provides methods and access to all data and household.
 * 
 * @author Robyer
 */
public final class Controller {

	public static final String TAG = Controller.class.getSimpleName();

	/** This singleton instance. */
	private static Controller mController;

	/** Application context */
	private final Context mContext;

	/** Persistence service for caching purposes */
	private final Persistence mPersistence;

	/** Network service for communication with server */
	private final INetwork mNetwork;

	/** Household object holds logged in user and all adapters and lists which belongs to him */
	private final Household mHousehold;

	/** Switch for using demo mode (with example adapter, without server) */
	private static boolean mDemoMode = false;

	/**
	 * Return singleton instance of this Controller. This is thread-safe.
	 * 
	 * @param context
	 *            This must be the global application context.
	 * @return singleton instance of controller
	 */
	public static Controller getInstance(Context context) {
		if (mController == null) {
			synchronized (Controller.class) {
				if (mController == null) {
					mController = new Controller(context);
				}
			}
		}

		return mController;
	}

	/**
	 * Private constructor.
	 * 
	 * @param context
	 *            This must be the global application context.
	 */
	private Controller(Context context) {
		mContext = context;

		mNetwork = mDemoMode ? new DemoNetwork(context) : new Network(mContext, this, "userID", Utils.isDebugVersion(context)); //FIXME: ROB: if you have user id, give me, else call put something and call getUID method
		mPersistence = new Persistence(mContext);
		mHousehold = new Household(mContext, mNetwork);
		mNetwork.setUser(mHousehold.user);
	}

	/**
	 * @param context
	 *            This must be the global Application context.
	 * @param demoMode
	 */
	public static synchronized void setDemoMode(Context context, boolean demoMode) {
		if (mDemoMode == demoMode)
			return;

		mDemoMode = demoMode;
		mController = new Controller(context);

		if (demoMode) {
			// Initialize default settings for demo mode, because in demo mode we don't call login()
			mController.mPersistence.initializeDefaultSettings(mController.mHousehold.user.getEmail());
		}
	}

	public static boolean isDemoMode() {
		return mDemoMode;
	}

	/** Persistence methods *************************************************/

	public String getLastEmail() {
		return mPersistence.loadLastEmail();
	}

	/**
	 * Get SharedPreferences for actually logged in user
	 * 
	 * @return null if user is not logged in
	 */
	public SharedPreferences getUserSettings() {
		String userEmail = mHousehold.user.getEmail();
		if (userEmail == null || userEmail.isEmpty()) {
			return null;
		}

		return mPersistence.getSettings(userEmail);
	}

	/** Communication methods ***********************************************/

	/**
	 * Login user by his email (authenticate on server).
	 * 
	 * @param email
	 * @return true on success, false otherwise
	 * @throws NetworkException
	 */
	public boolean login(String email) throws NetworkException {
		// TODO: catch and throw proper exception
		// FIXME: after some time there should be picture in ActualUser object, should save to mPersistence
		try {
			// We don't use UID in demo mode  
			if ((mNetwork instanceof Network) && !((Network)mNetwork).getUID(email)) {
				return false;
			}
			
			if (mNetwork.signIn(email, getGCMRegistrationId())) { // FIXME: gcmid have to be set separate now!!!, and here use getUID if you dont have userID
				mPersistence.saveLastEmail(email);
				mPersistence.initializeDefaultSettings(email);
				return true;
			}
		} catch (FalseException e) {
			// FIXME: ROB, do this how you want, this is working code :)
			switch (e.getDetail().getErrCode()) {
			case 0:
				break;
			case 2: //FIXME: there are constants of error in Constants.java file
			case 3: // bad token or email
				try {
					// TODO: do this otherway
					// GoogleAuth ggAuth = GoogleAuth.getGoogleAuth();
					//
					// ggAuth.invalidateToken();
					// ggAuth.doInForeground((ggAuth.getPictureIMG() == null)? true:false);

					if (mNetwork instanceof Network) {
						((Network) mNetwork).startGoogleAuth(true, getActualUser().isPictureDefault() ? true : false);
{'fraction_non_alphanumeric': 0.12237144371797137, 'fraction_numerical': 0.0005301289980561937, 'mean_word_length': 3.925587467362924, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3308600', 'n_tokens_mistral': 3742, 'n_tokens_neox': 3357, 'n_words': 940}