Android Open Source - Android-Activity-Tracker-for-Dynamics-CRM Setup Activity






From Project

Back to project page Android-Activity-Tracker-for-Dynamics-CRM.

License

The source code is released under:

MIT License

If you think the Android project Android-Activity-Tracker-for-Dynamics-CRM 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

// Android Activity Tracker Sample app for Microsoft Dynamics CRM
///*from w  w w.  java 2 s .  c o m*/
// Copyright (c) Microsoft Corporation
// All rights reserved.
// MIT License
//
// 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.microsoft.activitytracker.Activities;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.microsoft.activitytracker.Classes.ActivityTracker;
import com.microsoft.activitytracker.Classes.Constants;
import com.microsoft.activitytracker.Core.NetworkCalls;
import com.microsoft.activitytracker.R;
import com.microsoft.aad.adal.AuthenticationCallback;
import com.microsoft.aad.adal.AuthenticationContext;
import com.microsoft.aad.adal.AuthenticationResult;

import org.apache.http.message.BasicHttpResponse;

public class SetupActivity extends Activity implements View.OnClickListener,
        AuthenticationCallback<AuthenticationResult>
{
    private static final String TAG = "ActivityLogger.SetupActivity";
    private AuthenticationContext mAuthContext;
    private SharedPreferences mAppPrefs;
    private String mEndpoint;
    private String mUsername;
    private ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup);

        mAppPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        findViewById(R.id.login_button).setOnClickListener(this);

        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setProgressStyle(R.style.DialogProgressTheme);

        setEndpoint();
    }

    /**
     * set the text that populates the edittext. If the user already has an endpoint
     * saved then use that, otherwise we are going to use the default sample endpoint
     */
    private void setEndpoint() {
        String endpoint = mAppPrefs.getString(Constants.ENDPOINT, "");

        if (endpoint.equals("")) {
            endpoint = Constants.DEFAULT_ENDPOINT;
        }

        ((EditText)findViewById(R.id.endpoint_text)).setText(endpoint);
    }

    @Override
    public void onClick(View v)
    {
        mEndpoint = ((TextView)findViewById(R.id.endpoint_text)).getText().toString();
        mUsername = ((TextView)findViewById(R.id.username_text)).getText().toString();

        Object response = NetworkCalls.getAuthority(mEndpoint);
        if (response != null) {
            String authHeader = ((BasicHttpResponse) response).getHeaders("WWW-Authenticate")[0].toString();
            mAppPrefs.edit().putString(
                    Constants.AUTHORITY,
                    authHeader.substring(authHeader.indexOf("https://"))).apply();

            login();
        }
        else {
            Toast.makeText(this, "Unable to challenge service", Toast.LENGTH_SHORT).show();
        }
    }

    private void login() {
        String authority = mAppPrefs.getString(Constants.AUTHORITY,"");
        if (!authority.equals("")) {
            try {

                mAuthContext = new AuthenticationContext(SetupActivity.this, authority, false);
                mProgressDialog.show();

                // use the endpoint that the user entered in and use the username,
                // as the hint that is automatically populated into the azure library's webview
                mAuthContext.acquireToken(
                        SetupActivity.this,
                        mEndpoint,
                        Constants.CLIENT_ID,
                        Constants.REDIRECT_URI,
                        mUsername,
                        this
                );
            } catch (Exception ex) {
                ex.getCause().printStackTrace();
            }
        }
        else {
            Toast.makeText(this, getString(R.string.authority_error), Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (mAuthContext != null) {
            if (resultCode != 2001) {
                mAuthContext.onActivityResult(requestCode, resultCode, data);
            }
            else {
                mProgressDialog.hide();
                CookieSyncManager.createInstance(SetupActivity.this);
                CookieManager cookieManager = CookieManager.getInstance();
                cookieManager.removeAllCookie();
                CookieSyncManager.getInstance().sync();
                mAuthContext.getCache().removeAll();
                Toast.makeText(this, getString(R.string.login_error), Toast.LENGTH_LONG).show();
            }
        }
    }

    /**
     * if the login is successful, save the current token into the property created for the application,
     * store the username and endpoint into application shared storage, and return to the main activity
     * @param result returns <T> the result of the authentication
     */
    @Override
    public void onSuccess(AuthenticationResult result)
    {
        ActivityTracker.setCurrentSessionToken(getApplicationContext(), result.getAccessToken());

        mAppPrefs.edit().putString(Constants.REFRESH_TOKEN, result.getRefreshToken()).apply();
        mAppPrefs.edit().putString(Constants.ENDPOINT, mEndpoint).apply();
        mAppPrefs.edit().putString(Constants.USERNAME, mUsername).apply();

        finish();
    }

    /**
     * (currently just crash the app so we can see the full stacktrace in the logcat)
     * @param ex the exception that was thrown during login
     */
    @Override
    public void onError(Exception ex)
    {
        Log.d(TAG, ex.getCause().getStackTrace().toString());
//        ex.getCause().printStackTrace();
    }

    @Override
    public void onBackPressed()
    {
        // since we require them to be logged in for everything,
        // user can't leave this page unless they successfully log in.
    }
}




Java Source Code List

com.microsoft.aad.adal.ADALError.java
com.microsoft.aad.adal.AuthenticationActivity.java
com.microsoft.aad.adal.AuthenticationCallback.java
com.microsoft.aad.adal.AuthenticationCancelError.java
com.microsoft.aad.adal.AuthenticationConstants.java
com.microsoft.aad.adal.AuthenticationContext.java
com.microsoft.aad.adal.AuthenticationException.java
com.microsoft.aad.adal.AuthenticationParameters.java
com.microsoft.aad.adal.AuthenticationRequestState.java
com.microsoft.aad.adal.AuthenticationRequest.java
com.microsoft.aad.adal.AuthenticationResult.java
com.microsoft.aad.adal.AuthenticationSettings.java
com.microsoft.aad.adal.BrokerProxy.java
com.microsoft.aad.adal.CacheKey.java
com.microsoft.aad.adal.ChallangeResponseBuilder.java
com.microsoft.aad.adal.DefaultTokenCacheStore.java
com.microsoft.aad.adal.Discovery.java
com.microsoft.aad.adal.ExceptionExtensions.java
com.microsoft.aad.adal.FileTokenCacheStore.java
com.microsoft.aad.adal.HashMapExtensions.java
com.microsoft.aad.adal.HttpWebRequest.java
com.microsoft.aad.adal.HttpWebResponse.java
com.microsoft.aad.adal.IBrokerProxy.java
com.microsoft.aad.adal.IConnectionService.java
com.microsoft.aad.adal.IDeviceCertificate.java
com.microsoft.aad.adal.IDiscovery.java
com.microsoft.aad.adal.IJWSBuilder.java
com.microsoft.aad.adal.ITokenCacheStore.java
com.microsoft.aad.adal.ITokenStoreQuery.java
com.microsoft.aad.adal.IWebRequestHandler.java
com.microsoft.aad.adal.IdToken.java
com.microsoft.aad.adal.JWSBuilder.java
com.microsoft.aad.adal.Logger.java
com.microsoft.aad.adal.MemoryTokenCacheStore.java
com.microsoft.aad.adal.Oauth2.java
com.microsoft.aad.adal.PRNGFixes.java
com.microsoft.aad.adal.PackageHelper.java
com.microsoft.aad.adal.PromptBehavior.java
com.microsoft.aad.adal.StorageHelper.java
com.microsoft.aad.adal.StringExtensions.java
com.microsoft.aad.adal.TokenCacheItem.java
com.microsoft.aad.adal.UserInfo.java
com.microsoft.aad.adal.WebRequestHandler.java
com.microsoft.aad.adal.package-info.java
com.microsoft.activitytracker.Activities.CheckInActivity.java
com.microsoft.activitytracker.Activities.ItemActivity.java
com.microsoft.activitytracker.Activities.MainActivity.java
com.microsoft.activitytracker.Activities.SetupActivity.java
com.microsoft.activitytracker.Adapters.ActivitiesItemAdapter.java
com.microsoft.activitytracker.Adapters.MainItemAdapter.java
com.microsoft.activitytracker.Classes.ActivityTracker.java
com.microsoft.activitytracker.Classes.Constants.java
com.microsoft.activitytracker.Classes.Entity.java
com.microsoft.activitytracker.Classes.RecentHistorydbHandler.java
com.microsoft.activitytracker.Classes.Utils.java
com.microsoft.activitytracker.Core.NetworkCalls.java
com.microsoft.activitytracker.Core.SoapExecuteParser.java
com.microsoft.activitytracker.Core.SoapRetrieveMultipleParser.java