Android Open Source - etalio-android-sdk Main Activity






From Project

Back to project page etalio-android-sdk.

License

The source code is released under:

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions...

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

/*
 * Copyright 2014 Ericsson AB/*from  w  w  w.  j  ava  2  s. com*/
 *
 * 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.etalio.android.sample;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.etalio.android.AuthenticationCallback;
import com.etalio.android.EtalioExtendedAPI;
import com.etalio.android.client.exception.EtalioAuthorizationCodeException;
import com.etalio.android.client.exception.EtalioHttpException;
import com.etalio.android.client.exception.EtalioTokenException;
import com.etalio.android.client.models.PromotedApplications;
import com.etalio.android.client.models.User;

import java.io.IOException;

public class MainActivity extends ActionBarActivity implements AuthenticationCallback {

    private static final String TAG = MainActivity.class.getCanonicalName();

    private static final String CLIENT_ID = "e71f0f0d296f987116f7d107f17af3d9";
    private static final String CLIENT_SECRET = "d4c5d046dc18a1263d17f35ccb3731a6";
//    private static final String CLIENT_ID = "0b5c59003c151789b3bd13cb9de95d9d";
//    private static final String CLIENT_SECRET = "b5b971f4340382650a559912f0e39e79";

    private EtalioExtendedAPI mEtalio;

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
        }

        //Initiate Etalio API
        mEtalio = new EtalioExtendedAPI(this, CLIENT_ID, CLIENT_SECRET, "https://api-etalio.3fs.si/", "v1");
        //Handle callbacks for Etalio authorization
        try {
            boolean isCallback = mEtalio.handleSignInCallback(getIntent(), this);
            Log.d(TAG, "Intent is " + (isCallback ? "" : "not") + " a Etalio authentication callback");
        } catch (EtalioAuthorizationCodeException e) {
            setText(e.getMessage());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        maybeEnableProfileButton();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //Handle result from native SSO which occurs if Etalio app is installed.
        mEtalio.onActivityResult(requestCode, resultCode, data, this);
    }

    private void maybeEnableProfileButton() {
        findViewById(R.id.buttonProfile).setEnabled(true);
        findViewById(R.id.buttonProfileTwoFactor).setEnabled(true);
        findViewById(R.id.promotedApps).setEnabled(true);
    }

    /**
     * Methods listening to sign in button click.
     * @param v View that trigger sign in
     */
    public void onSignInClick(View v) {
        mEtalio.initiateEtalioSignIn(this, "profile.basic.r profile.email.r");
    }

    public void onPromotedAppsClick(View v){
        new AsyncTask<Void, Void, PromotedApplications>() {

            @Override
            protected PromotedApplications doInBackground(Void... params) {
                try {
                    return mEtalio.getPromotedApplications();
                } catch (EtalioTokenException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (EtalioHttpException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(PromotedApplications applications) {
                if(applications != null){
                    String text = String.format("There are %d apps and the first is called %s", applications.getList().length, applications.getList().length > 0 ? applications.getList()[0].getLabel() : "?");
                    setText(text);
                } else {
                    setText(String.format("Error loading promoted apps"));
                }
            }
        }.execute();
    }

    /**
     * Methods listening to the get profile button click.
     * @param v View that trigger profile fetching
     */
    public void onProfileClick(View v) {
        new AsyncTask<Void, Void, User>() {

            @Override
            protected User doInBackground(Void... params) {
                try {
                    return mEtalio.getCurrentProfile();
                } catch (IOException e) {
                    Log.e(TAG, "Error requesting profile - " + e.getMessage(), e);
                    return null;
                } catch (EtalioTokenException e) {
                    Log.e(TAG, "Error requesting profile - " + e.getMessage(), e);
                    return null;
                } catch (EtalioHttpException e) {
                    Log.e(TAG, "Error requesting profile - " + "(" + e.getResponseCode() +")" + e.getResponseMessage(), e);
                    return null;
                }
            }

            @Override
            protected void onPostExecute(User user) {
                if(user != null) {
                    String text = user.getName() + " (" + user.getPhoneNumber() +")";
                    setText(text);
                } else {
                    setText("Error getting profile.");
                }
            }
        }.execute();
    }

    public void onProfileTwoFactorClick(View view) {
        new AsyncTask<Void, Void, User>(){

            @Override
            protected User doInBackground(Void... voids) {
                try{
                    User user = mEtalio.getCurrentProfile();
                    User update = new User(user.getId());
                    update.set2FactorSecurity(true);
                    return mEtalio.updateProfileSecurity(update);
                } catch (EtalioTokenException e) {
                    e.printStackTrace();
                } catch (EtalioHttpException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(User user) {
                if(user != null) {
                    String text = user.getName() + " (" + user.getPhoneNumber() +")" + " has security enabled:" + user.get2FactorSecurity();
                    setText(text);
                } else {
                    setText("Error getting profile.");
                }
            }

        }.execute();
    }

    private void setText(String text) {
        TextView tv = ((TextView)findViewById(R.id.textviewDebug));
        if(tv != null)
        {
            tv.setText(text);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onAuthenticationSuccess() {
        setText("Authentication succeeded!");
        maybeEnableProfileButton();
    }

    @Override
    public void onAuthenticationFailure(Exception exception) {
        setText("Authentication failed :-( - " + exception.getMessage());
    }



    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
}




Java Source Code List

com.etalio.android.AuthenticationCallback.java
com.etalio.android.EtalioAPI.java
com.etalio.android.EtalioBase.java
com.etalio.android.EtalioExtendedAPI.java
com.etalio.android.EtalioPersistentStore.java
com.etalio.android.Utils.java
com.etalio.android.client.DefaultEtalioHttpClient.java
com.etalio.android.client.GsonHttpBodyConverter.java
com.etalio.android.client.HttpBodyConverter.java
com.etalio.android.client.HttpClient.java
com.etalio.android.client.HttpRequest.java
com.etalio.android.client.HttpResponse.java
com.etalio.android.client.exception.EtalioAuthorizationCodeException.java
com.etalio.android.client.exception.EtalioHttpError.java
com.etalio.android.client.exception.EtalioHttpException.java
com.etalio.android.client.exception.EtalioProfileExpiredException.java
com.etalio.android.client.exception.EtalioTokenException.java
com.etalio.android.client.models.Action.java
com.etalio.android.client.models.Application.java
com.etalio.android.client.models.Country.java
com.etalio.android.client.models.EtalioToken.java
com.etalio.android.client.models.NewUser.java
com.etalio.android.client.models.Operator.java
com.etalio.android.client.models.PromotedApplications.java
com.etalio.android.client.models.Status.java
com.etalio.android.client.models.TokenResponse.java
com.etalio.android.client.models.UserUpdate.java
com.etalio.android.client.models.User.java
com.etalio.android.client.models.Version.java
com.etalio.android.client.models.requests.ApplicationAdd.java
com.etalio.android.client.models.requests.CreateApplication.java
com.etalio.android.client.models.requests.CreateProfile.java
com.etalio.android.client.models.requests.MsisdnRequestClaim.java
com.etalio.android.client.models.requests.MsisdnRequestCodeResponse.java
com.etalio.android.client.models.requests.MsisdnResponseCodeResponse.java
com.etalio.android.client.models.requests.MsisdnResponseCode.java
com.etalio.android.client.models.requests.MsisdnVerifyToken.java
com.etalio.android.client.models.requests.Msisdn.java
com.etalio.android.client.models.requests.PasswordReset.java
com.etalio.android.client.models.requests.ProfileApplicationAdd.java
com.etalio.android.client.models.requests.ProfileSms.java
com.etalio.android.sample.MainActivity.java
com.etalio.android.util.Log.java
com.etalio.android.util.MimeTypeUtil.java