Android Open Source - steamchat Authentication Activity






From Project

Back to project page steamchat.

License

The source code is released under:

Apache License

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

/*
 * 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
 *//w w w. j  av  a  2 s. c  om
 *      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.kevelbreh.steamchat.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.kevelbreh.steamchat.R;
import com.kevelbreh.steamchat.SteamChat;
import com.kevelbreh.steamchat.steam.language.Language;
import com.kevelbreh.steamchat.steam2.SteamService;

import org.apache.commons.lang.StringUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Authenticate a user to the steam network.  This activity is responsible for logging in the user
 * using a username and password.  If this is a first time login using this device, steam guard will
 * probably want the user to authenticate the device also.
 *
 * TODO: Add spinner when performing a network operation (logging in attempts).
 */
public class AuthenticationActivity extends Activity {

    private boolean guarded = false;

    @InjectView(R.id.credentials_container) ViewGroup mCredentialsContainer;
    @InjectView(R.id.guard_container) ViewGroup mGuardContainer;
    @InjectView(R.id.authenticate) Button mAuthenticate;
    @InjectView(R.id.username) EditText mUsername;
    @InjectView(R.id.password) EditText mPassword;
    @InjectView(R.id.guard) EditText mGuard;
    @InjectView(R.id.machine) EditText mMachine;

    /**
     * Service connection which connects to {@link com.kevelbreh.steamchat.steam.SteamService} for
     * us to send messages.  This can also be used for receiving the "typing..." signal.
     */
    private Messenger mService = null;
    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            mService = new Messenger(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName className) {
            mService = null;
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login_2);
        ButterKnife.inject(this);
        bindService(new Intent(this, SteamService.class), mConnection, Context.BIND_AUTO_CREATE);
        mAuthenticate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                authenticate();
            }
        });
    }

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);

        switch (intent.getIntExtra("result", -500)) {
            // Login was successful!
            case Language.Result.OK:
                startActivity(new Intent(this, InteractionsActivity.class));
                finish();
                break;
            // Steam requires the user to provide the Steam Guard code emailed to their email address
            // registered on Steam.
            case Language.Result.ACCOUNT_LOGON_DENIED:
                setupGuardState();
                break;
            // Some other error occurred.  Display this to the user for their viewing. Possibly reset
            // the login screen.
            default:
                setupNormalState();
                Toast.makeText(this, "Something went wrong.", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbindService(mConnection);
    }

    /**
     * Authenticate the user.  Perform actions depending on the required form state and result code
     * of the Steam API.
     */
    @OnClick(R.id.authenticate)
    public void authenticate() {
        if (isValid()) try {
            Bundle data = new Bundle();
            data.putString("username", getUsername());
            data.putString("password", getPassword());
            data.putString("guard", getGuard());
            data.putString("machine", getMachine());

            Message message = Message.obtain(null, SteamService.EVENT_STEAM_USER_LOGIN);
            message.setData(data);
            mService.send(message);
        }
        catch(RemoteException e) {
            SteamChat.debug(this, e.getMessage(), e);
        }
    }

    /**
     * Setup the form to show only the required fields for a steam guard protected login.
     */
    private void setupGuardState() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                guarded = true;
                mGuardContainer.setVisibility(View.VISIBLE);
                mCredentialsContainer.setVisibility(View.GONE);

                if (StringUtils.isEmpty(android.os.Build.MODEL) && android.os.Build.MODEL.length() >= 6) {
                    mMachine.setText(android.os.Build.MODEL);
                }
            }
        });
    }

    /**
     * Setup the form to show only the required fields for a basic login.
     */
    private void setupNormalState() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                guarded = false;
                mCredentialsContainer.setVisibility(View.VISIBLE);
                mGuardContainer.setVisibility(View.GONE);
                mPassword.setText(null);
            }
        });
    }

    /**
     * @return whether the form is valid or not.
     */
    private boolean isValid() {
        boolean hasError = false;

        if (mUsername.getText().length() == 0) {
            mUsername.setError("Enter your username.");
            hasError = true;
        }

        if (mPassword.getText().length() == 0) {
            mPassword.setError("Enter your password.");
            hasError = true;
        }

        if (guarded && mGuard.getText().length() == 0) {
            mGuard.setError("Enter the Steam Guard code that has been sent to your email.");
            hasError = true;
        }

        if (guarded && mGuard.getText().length() != 5) {
            mGuard.setError("The Steam Guard code sent to you can only be 5 characters in length.");
            hasError = true;
        }

        if (guarded && mMachine.getText().length() == 0) {
            mMachine.setError("Enter a name to identify this connecting machine.");
            hasError = true;
        }

        if (guarded && mMachine.getText().length() < 6) {
            mMachine.setError("The machine name needs to be longer than 6 characters.");
            hasError = true;
        }

        return !hasError;
    }

    /**
     * @return the entered username
     */
    private String getUsername() {
        return mUsername.getText().toString();
    }

    /**
     * @return the entered password
     */
    private String getPassword() {
        return mPassword.getText().toString();
    }

    /**
     * @return the entered steam guard code or null.
     */
    private String getGuard() {
        final String temp = mGuard.getText().toString();
        return StringUtils.isEmpty(temp) ? null : temp;
    }

    /**
     * @return the entered users machine name or null.
     */
    private String getMachine() {
        final String temp = mMachine.getText().toString();
        return StringUtils.isEmpty(temp) ? null : temp;
    }
}




Java Source Code List

com.kevelbreh.steamchat.SteamChat.java
com.kevelbreh.steamchat.account.AuthenticatorService.java
com.kevelbreh.steamchat.account.Authenticator.java
com.kevelbreh.steamchat.account.SteamAccount.java
com.kevelbreh.steamchat.activity.AuthenticationActivity.java
com.kevelbreh.steamchat.activity.ConversationActivity.java
com.kevelbreh.steamchat.activity.FriendsActivity.java
com.kevelbreh.steamchat.activity.InteractionsActivity.java
com.kevelbreh.steamchat.activity.LauncherActivity.java
com.kevelbreh.steamchat.activity.SettingsActivity.java
com.kevelbreh.steamchat.fragment.ConversationFragment.java
com.kevelbreh.steamchat.fragment.FriendsFragment.java
com.kevelbreh.steamchat.fragment.InteractionsFragment.java
com.kevelbreh.steamchat.fragment.SettingsFragment.java
com.kevelbreh.steamchat.provider.SteamProviderUtils.java
com.kevelbreh.steamchat.provider.SteamProvider.java
com.kevelbreh.steamchat.provider.content.InteractionContentItem.java
com.kevelbreh.steamchat.provider.content.PersonaContentItem.java
com.kevelbreh.steamchat.provider.content.UserContentItem.java
com.kevelbreh.steamchat.steam2.SteamConnection.java
com.kevelbreh.steamchat.steam2.SteamEventBus.java
com.kevelbreh.steamchat.steam2.SteamService.java
com.kevelbreh.steamchat.steam2.handler.ConnectionHandler.java
com.kevelbreh.steamchat.steam2.handler.FriendHandler.java
com.kevelbreh.steamchat.steam2.handler.MessageHandler.java
com.kevelbreh.steamchat.steam2.handler.UserHandler.java
com.kevelbreh.steamchat.steam2.packet.Packet.java
com.kevelbreh.steamchat.steam2.packet.ProtoPacket.java
com.kevelbreh.steamchat.steam.SteamClient.java
com.kevelbreh.steamchat.steam.SteamID.java
com.kevelbreh.steamchat.steam.SteamServiceHandler.java
com.kevelbreh.steamchat.steam.SteamService.java
com.kevelbreh.steamchat.steam.handler2.ConnectionHandler.java
com.kevelbreh.steamchat.steam.handler2.FriendHandler.java
com.kevelbreh.steamchat.steam.handler2.Handler.java
com.kevelbreh.steamchat.steam.handler2.UserHandler.java
com.kevelbreh.steamchat.steam.handler.AEventHandler.java
com.kevelbreh.steamchat.steam.handler.AuthenticationHandler.java
com.kevelbreh.steamchat.steam.handler.FriendsHandler.java
com.kevelbreh.steamchat.steam.handler.IEventHandler.java
com.kevelbreh.steamchat.steam.handler.IHandler.java
com.kevelbreh.steamchat.steam.handler.MessageDebugHandler.java
com.kevelbreh.steamchat.steam.language.Language.java
com.kevelbreh.steamchat.steam.language.Message.java
com.kevelbreh.steamchat.steam.network.TCPConnection.java
com.kevelbreh.steamchat.steam.network.packet.ChannelEncryptRequest.java
com.kevelbreh.steamchat.steam.network.packet.ChannelEncryptResponse.java
com.kevelbreh.steamchat.steam.network.packet.ChannelEncryptResult.java
com.kevelbreh.steamchat.steam.network.packet.ClientLogOnResponse.java
com.kevelbreh.steamchat.steam.network.packet.ClientLogOn.java
com.kevelbreh.steamchat.steam.network.packet.HeartBeat.java
com.kevelbreh.steamchat.steam.network.packet.MultiPacket.java
com.kevelbreh.steamchat.steam.network.packet.Packet.java
com.kevelbreh.steamchat.steam.network.packet.ProtoPacket.java
com.kevelbreh.steamchat.steam.proto.DescriptorsProto.java
com.kevelbreh.steamchat.steam.proto.EncryptedAppTicketProto.java
com.kevelbreh.steamchat.steam.proto.SteamMessagesBaseProto.java
com.kevelbreh.steamchat.steam.proto.SteamMessagesClientServerProto.java
com.kevelbreh.steamchat.steam.security.AsnKeyParser.java
com.kevelbreh.steamchat.steam.security.AsnParser.java
com.kevelbreh.steamchat.steam.security.BerDecodeException.java
com.kevelbreh.steamchat.steam.security.Cryptography.java
com.kevelbreh.steamchat.steam.security.NetEncryption.java
com.kevelbreh.steamchat.steam.security.PublicKey.java
com.kevelbreh.steamchat.steam.security.RSA.java
com.kevelbreh.steamchat.steam.util.BinaryReader.java
com.kevelbreh.steamchat.steam.util.BinaryWriter.java
com.kevelbreh.steamchat.steam.util.BitVector64.java
com.kevelbreh.steamchat.steam.util.JenkinsHash.java
com.kevelbreh.steamchat.util.AServiceActivity.java
com.kevelbreh.steamchat.util.Dump.java
com.kevelbreh.steamchat.util.MiscUtils.java
com.kevelbreh.steamchat.widget.adapter.ChatAdapter.java
com.kevelbreh.steamchat.widget.adapter.ConversationAdapter.java
com.kevelbreh.steamchat.widget.adapter.FriendAdapter.java
com.kevelbreh.steamchat.widget.view.AvatarView.java
com.kevelbreh.steamchat.widget.view.FriendInteractionsView.java