Android Open Source - force_analytics_example Boot Config






From Project

Back to project page force_analytics_example.

License

The source code is released under:

Copyright (c) 2011, salesforce.com, inc. All rights reserved. ======================================== Redistribution and use of this software in source and binary forms, with or without modificatio...

If you think the Android project force_analytics_example 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, salesforce.com, inc.
 * All rights reserved./*from   w  ww . j  a  v  a 2  s .  com*/
 * Redistribution and use of this software in source and binary forms, with or
 * without modification, are permitted provided that the following conditions
 * are met:
 * - Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * - Neither the name of salesforce.com, inc. nor the names of its contributors
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission of salesforce.com, inc.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package com.salesforce.androidsdk.rest;

import java.io.IOException;
import java.util.Scanner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.salesforce.androidsdk.R;
import com.salesforce.androidsdk.app.SalesforceSDKManager;

import android.content.Context;
import android.content.res.Resources;

/**
 * Class encapsulating the application configuration (consumer key, oauth scopes, refresh behavior).
 */
public class BootConfig {

  // We expect a assets/www/bootconfig.json file to be provided by hybrid apps.
  private static final String HYBRID_BOOTCONFIG_PATH = "www/bootconfig.json";

  // bootconfig.json should contain a map with the following keys.
  private static final String REMOTE_ACCESS_CONSUMER_KEY = "remoteAccessConsumerKey";
  private static final String OAUTH_REDIRECT_URI = "oauthRedirectURI";
  private static final String OAUTH_SCOPES = "oauthScopes";
  private static final String IS_LOCAL = "isLocal";
  private static final String START_PAGE = "startPage";
  private static final String ERROR_PAGE = "errorPage";
  private static final String SHOULD_AUTHENTICATE = "shouldAuthenticate";
  private static final String ATTEMPT_OFFLINE_LOAD = "attemptOfflineLoad";
  private static final String PUSH_NOTIFICATION_CLIENT_ID = "androidPushNotificationClientId";

  // Default for optional configs.
  private static final boolean DEFAULT_SHOULD_AUTHENTICATE = true;
  private static final boolean DEFAULT_ATTEMPT_OFFLINE_LOAD = true;

  private String remoteAccessConsumerKey;
  private String oauthRedirectURI;
  private String[] oauthScopes;
  private boolean isLocal;
  private String startPage;
  private String errorPage;
  private boolean shouldAuthenticate;
  private boolean attemptOfflineLoad;
  private String pushNotificationClientId;

  private static BootConfig INSTANCE = null;

  /**
     * Method to (build and) get the singleton instance.
     *
   * @param ctx Context.
   * @return BootConfig instance.
   */
  public static BootConfig getBootConfig(Context ctx) {
    if (INSTANCE == null) {
      INSTANCE = new BootConfig();
      if (SalesforceSDKManager.getInstance().isHybrid()) {
        INSTANCE.readFromJSON(ctx);
      } else {
        INSTANCE.readFromXML(ctx);
      }
    }
    return INSTANCE;
  }

  /**
   * Initializes this BootConfig object by reading the content of bootconfig.json.
   *
   * @param ctx Context.
   */
  private void readFromJSON(Context ctx) {
    final String jsonStr = readBootConfigFile(ctx);
    parseBootConfigStr(jsonStr);
  }

  /**
   * Initializes this BootConfig object by reading the config from XML.
   *
   * @param ctx Context.
   */
  private void readFromXML(Context ctx) {
    final Resources res = ctx.getResources();
    remoteAccessConsumerKey = res.getString(R.string.remoteAccessConsumerKey);
    oauthRedirectURI = res.getString(R.string.oauthRedirectURI);
    oauthScopes = res.getStringArray(R.array.oauthScopes);
    pushNotificationClientId = res.getString(R.string.androidPushNotificationClientId);
  }

  /**
   * Reads the contents of the boot config file.
   *
   * @param ctx Context.
   * @return String content of bootconfig.json.
   */
  private String readBootConfigFile(Context ctx) {
    Scanner scanner = null;
    try {
      scanner = new Scanner(ctx.getAssets().open(HYBRID_BOOTCONFIG_PATH));

      // Good trick to get a string from a stream (http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html).
      return scanner.useDelimiter("\\A").next();
    } catch (IOException e) {
      throw new BootConfigException("Failed to open " + HYBRID_BOOTCONFIG_PATH, e);
    } finally {
      if (scanner != null) {
        scanner.close();
      }
    }
  }

  /**
   * Initializes this BootConfig object by parsing a JSON string.
   *
   * @param jsonStr JSON string.
   */
  private void parseBootConfigStr(String jsonStr) {
    try {
      final JSONObject config = new JSONObject(jsonStr);

      // Required fields.
      remoteAccessConsumerKey = config.getString(REMOTE_ACCESS_CONSUMER_KEY);
      oauthRedirectURI = config.getString(OAUTH_REDIRECT_URI);
      final JSONArray jsonScopes = config.getJSONArray(OAUTH_SCOPES);
      oauthScopes = new String[jsonScopes.length()];
      for (int i=0; i<oauthScopes.length; i++) {
        oauthScopes[i] = jsonScopes.getString(i);
      }
      isLocal = config.getBoolean(IS_LOCAL);
      startPage = config.getString(START_PAGE);
      errorPage = config.getString(ERROR_PAGE);

      // Optional fields.
      pushNotificationClientId = config.optString(PUSH_NOTIFICATION_CLIENT_ID);
      shouldAuthenticate = config.optBoolean(SHOULD_AUTHENTICATE, DEFAULT_SHOULD_AUTHENTICATE);
      attemptOfflineLoad = config.optBoolean(ATTEMPT_OFFLINE_LOAD, DEFAULT_ATTEMPT_OFFLINE_LOAD);
    } catch (JSONException e) {
      throw new BootConfigException("Failed to parse " + HYBRID_BOOTCONFIG_PATH, e);
    }
  }

  /**
   * Returns the consumer key value specified for your remote access object or connected app.
   *
   * @return Consumer key value specified for your remote access object or connected app.
   */
  public String getRemoteAccessConsumerKey() {
    return remoteAccessConsumerKey;
  }

  /**
   * Returns the redirect URI value specified for your remote access object or connected app.
   *
   * @return Redirect URI value specified for your remote access object or connected app.
   */
  public String getOauthRedirectURI() {
    return oauthRedirectURI;
  }

  /**
   * Returns the authorization/access scope(s) that the application needs to ask for at login.
   * @return Authorization/access scope(s) that the application needs to ask for at login.
   */
  public String[] getOauthScopes() {
    return oauthScopes;
  }

  /**
   * Returns if the start page is local or a VF page.
   *
   * @return True - if start page is in assets/www, False - if it's a VF page.
   */
  public boolean isLocal() {
    return isLocal;
  }

  /**
   * Returns the path to the start page (local or remote).
   * Example: index.html or /apex/basicpage.
   *
   * @return Path to start page (local or remote).
   */
  public String getStartPage() {
    return startPage;
  }

  /**
   * Returns the path to the local error page.
   *
   * @return Path to local error page.
   */
  public String getErrorPage() {
    return errorPage;
  }

  /**
   * Returns whether the app should go through login flow the first time or not.
   *
   * @return True - if the app should go through login flow, False - otherwise.
   */
  public boolean shouldAuthenticate() {
    return shouldAuthenticate;
  }

  /**
   * Returns whether the app should attempt to load cached content when offline.
   *
   * @return True - if the app should attempt to load cached content, False - otherwise.
   */
  public boolean attemptOfflineLoad() {
    return attemptOfflineLoad;
  }

  /**
   * Returns the push notification client ID.
   *
   * @return Push notification client ID.
   */
  public String getPushNotificationClientId() {
    return pushNotificationClientId;
  }

  /**
   * Exception thrown for all bootconfig parsing errors.
   */
  static public class BootConfigException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public BootConfigException(String msg, Throwable cause) {
      super(msg, cause);
    }
  }
}




Java Source Code List

com.salesforce.androidsdk.accounts.UserAccountManagerWithSmartStore.java
com.salesforce.androidsdk.accounts.UserAccountManager.java
com.salesforce.androidsdk.accounts.UserAccount.java
com.salesforce.androidsdk.app.SalesforceSDKManager.java
com.salesforce.androidsdk.app.UUIDManager.java
com.salesforce.androidsdk.app.UpgradeManager.java
com.salesforce.androidsdk.auth.AccountWatcher.java
com.salesforce.androidsdk.auth.AuthenticatorService.java
com.salesforce.androidsdk.auth.HttpAccess.java
com.salesforce.androidsdk.auth.LoginServerManager.java
com.salesforce.androidsdk.auth.OAuth2.java
com.salesforce.androidsdk.phonegap.ForcePlugin.java
com.salesforce.androidsdk.phonegap.JavaScriptPluginVersion.java
com.salesforce.androidsdk.phonegap.SDKInfoPlugin.java
com.salesforce.androidsdk.phonegap.SFAccountManagerPlugin.java
com.salesforce.androidsdk.phonegap.SalesforceOAuthPlugin.java
com.salesforce.androidsdk.phonegap.TestRunnerPlugin.java
com.salesforce.androidsdk.push.PushBroadcastReceiver.java
com.salesforce.androidsdk.push.PushMessaging.java
com.salesforce.androidsdk.push.PushNotificationInterface.java
com.salesforce.androidsdk.push.PushService.java
com.salesforce.androidsdk.rest.AdminPrefsManager.java
com.salesforce.androidsdk.rest.ApiVersionStrings.java
com.salesforce.androidsdk.rest.BootConfig.java
com.salesforce.androidsdk.rest.ClientManager.java
com.salesforce.androidsdk.rest.RestClient.java
com.salesforce.androidsdk.rest.RestRequest.java
com.salesforce.androidsdk.rest.RestResponse.java
com.salesforce.androidsdk.rest.files.ApiRequests.java
com.salesforce.androidsdk.rest.files.ConnectUriBuilder.java
com.salesforce.androidsdk.rest.files.FileRequests.java
com.salesforce.androidsdk.rest.files.RenditionType.java
com.salesforce.androidsdk.security.Encryptor.java
com.salesforce.androidsdk.security.PRNGFixes.java
com.salesforce.androidsdk.security.PasscodeManager.java
com.salesforce.androidsdk.smartstore.app.SalesforceSDKManagerWithSmartStore.java
com.salesforce.androidsdk.smartstore.app.UpgradeManagerWithSmartStore.java
com.salesforce.androidsdk.smartstore.phonegap.SmartStorePlugin.java
com.salesforce.androidsdk.smartstore.phonegap.StoreCursor.java
com.salesforce.androidsdk.smartstore.store.DBHelper.java
com.salesforce.androidsdk.smartstore.store.DBOpenHelper.java
com.salesforce.androidsdk.smartstore.store.IndexSpec.java
com.salesforce.androidsdk.smartstore.store.QuerySpec.java
com.salesforce.androidsdk.smartstore.store.SmartSqlHelper.java
com.salesforce.androidsdk.smartstore.store.SmartStore.java
com.salesforce.androidsdk.ui.AccountSwitcherActivity.java
com.salesforce.androidsdk.ui.CustomServerUrlEditor.java
com.salesforce.androidsdk.ui.LoginActivity.java
com.salesforce.androidsdk.ui.ManageSpaceActivity.java
com.salesforce.androidsdk.ui.OAuthWebviewHelper.java
com.salesforce.androidsdk.ui.PasscodeActivity.java
com.salesforce.androidsdk.ui.SalesforceAccountRadioButton.java
com.salesforce.androidsdk.ui.SalesforceR.java
com.salesforce.androidsdk.ui.SalesforceServerRadioButton.java
com.salesforce.androidsdk.ui.ServerPickerActivity.java
com.salesforce.androidsdk.ui.sfhybrid.SalesforceDroidGapActivity.java
com.salesforce.androidsdk.ui.sfhybrid.SalesforceGapViewClient.java
com.salesforce.androidsdk.ui.sfnative.SalesforceActivity.java
com.salesforce.androidsdk.ui.sfnative.SalesforceExpandableListActivity.java
com.salesforce.androidsdk.ui.sfnative.SalesforceListActivity.java
com.salesforce.androidsdk.util.EventsListenerQueue.java
com.salesforce.androidsdk.util.EventsObservable.java
com.salesforce.androidsdk.util.EventsObserver.java
com.salesforce.androidsdk.util.ForceAppInstrumentationTestCase.java
com.salesforce.androidsdk.util.HybridInstrumentationTestCase.java
com.salesforce.androidsdk.util.JSTestCase.java
com.salesforce.androidsdk.util.JUnitReportTestRunner.java
com.salesforce.androidsdk.util.LogUtil.java
com.salesforce.androidsdk.util.NativeInstrumentationTestCase.java
com.salesforce.androidsdk.util.TimeLimitedTestRunner.java
com.salesforce.androidsdk.util.TokenRevocationReceiver.java
com.salesforce.androidsdk.util.UriFragmentParser.java
com.salesforce.androidsdk.util.UserSwitchReceiver.java
com.salesforce.samples.accounteditor.AccountEditorApp.java
com.salesforce.samples.accounteditor.KeyImpl.java
com.salesforce.samples.analyticsapp.AnalyticsApp.java
com.salesforce.samples.analyticsapp.GraphActivity.java
com.salesforce.samples.analyticsapp.KeyImpl.java
com.salesforce.samples.analyticsapp.MainActivity.java
com.salesforce.samples.analyticsapp.PieChart.java
com.salesforce.samples.contactexplorer.ContactExplorerApp.java
com.salesforce.samples.contactexplorer.KeyImpl.java
com.salesforce.samples.hybridfileexplorer.HybridFileExplorerApp.java
com.salesforce.samples.hybridfileexplorer.KeyImpl.java
com.salesforce.samples.smartstoreexplorer.KeyImpl.java
com.salesforce.samples.smartstoreexplorer.SmartStoreExplorerApp.java
com.salesforce.samples.vfconnector.KeyImpl.java
com.salesforce.samples.vfconnector.VFConnectorApp.java