AccountActivity.java
上传用户:szyujian
上传日期:2016-09-20
资源大小:320k
文件大小:12k
源码类别:

android开发

开发平台:

C/C++

  1. /*
  2.  * Copyright (C) 2008 Esmertec AG.
  3.  * Copyright (C) 2008 The Android Open Source Project
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package com.android.im.app;
  18. import com.android.im.R;
  19. import com.android.im.plugin.BrandingResourceIDs;
  20. import android.app.Activity;
  21. import android.app.AlertDialog;
  22. import android.content.ContentResolver;
  23. import android.content.ContentUris;
  24. import android.content.ContentValues;
  25. import android.content.DialogInterface;
  26. import android.content.Intent;
  27. import android.database.Cursor;
  28. import android.graphics.drawable.Drawable;
  29. import android.net.Uri;
  30. import android.os.Bundle;
  31. import android.provider.Im;
  32. import android.text.Editable;
  33. import android.text.SpannableStringBuilder;
  34. import android.text.Spanned;
  35. import android.text.SpannableString;
  36. import android.text.TextUtils;
  37. import android.text.TextWatcher;
  38. import android.text.method.LinkMovementMethod;
  39. import android.text.style.URLSpan;
  40. import android.text.util.Linkify;
  41. import android.util.Log;
  42. import android.view.View;
  43. import android.view.Window;
  44. import android.view.View.OnClickListener;
  45. import android.widget.Button;
  46. import android.widget.CheckBox;
  47. import android.widget.CompoundButton;
  48. import android.widget.EditText;
  49. import android.widget.TextView;
  50. import android.widget.Toast;
  51. import android.widget.CompoundButton.OnCheckedChangeListener;
  52. public class AccountActivity extends Activity {
  53.     private static final String ACCOUNT_URI_KEY = "accountUri";
  54.     static final int REQUEST_SIGN_IN = RESULT_FIRST_USER + 1;
  55.     private static final String[] ACCOUNT_PROJECTION = {
  56.         Im.Account._ID,
  57.         Im.Account.PROVIDER,
  58.         Im.Account.USERNAME,
  59.         Im.Account.PASSWORD,
  60.         Im.Account.KEEP_SIGNED_IN,
  61.     };
  62.     private static final int ACCOUNT_PROVIDER_COLUMN = 1;
  63.     private static final int ACCOUNT_USERNAME_COLUMN = 2;
  64.     private static final int ACCOUNT_PASSWORD_COLUMN = 3;
  65.     private static final int ACCOUNT_KEEP_SIGNED_IN_COLUMN = 4;
  66.     Uri mAccountUri;
  67.     EditText mEditName;
  68.     EditText mEditPass;
  69.     CheckBox mRememberPass;
  70.     CheckBox mKeepSignIn;
  71.     Button   mBtnSignIn;
  72.     String mToAddress;
  73.     @Override
  74.     protected void onCreate(Bundle icicle) {
  75.         super.onCreate(icicle);
  76.         getWindow().requestFeature(Window.FEATURE_LEFT_ICON);
  77.         setContentView(R.layout.account_activity);
  78.         mEditName = (EditText)findViewById(R.id.edtName);
  79.         mEditPass = (EditText)findViewById(R.id.edtPass);
  80.         mRememberPass = (CheckBox)findViewById(R.id.rememberPassword);
  81.         mKeepSignIn = (CheckBox)findViewById(R.id.keepSignIn);
  82.         mBtnSignIn = (Button)findViewById(R.id.btnSignIn);
  83.         mRememberPass.setOnCheckedChangeListener(new OnCheckedChangeListener(){
  84.             public void onCheckedChanged(CompoundButton buttonView,
  85.                     boolean isChecked) {
  86.                 updateWidgetState();
  87.             }
  88.         });
  89.         ImApp app = ImApp.getApplication(this);
  90.         Intent i = getIntent();
  91.         String action = i.getAction();
  92.         mToAddress = i.getStringExtra(ImApp.EXTRA_INTENT_SEND_TO_USER);
  93.         final String origUserName;
  94.         final long providerId;
  95.         final ProviderDef provider;
  96.         if(Intent.ACTION_INSERT.equals(action)) {
  97.             origUserName = "";
  98.             providerId = ContentUris.parseId(i.getData());
  99.             provider = app.getProvider(providerId);
  100.             setTitle(getResources().getString(R.string.add_account, provider.mFullName));
  101.         } else if(Intent.ACTION_EDIT.equals(action)) {
  102.             ContentResolver cr = getContentResolver();
  103.             Uri uri = i.getData();
  104.             if ((uri == null) || !Im.Account.CONTENT_ITEM_TYPE.equals(cr.getType(uri))) {
  105.                 Log.w(ImApp.LOG_TAG, "<AccountActivity>Bad data");
  106.                 return;
  107.             }
  108.             Cursor cursor = cr.query(uri, ACCOUNT_PROJECTION, null, null, null);
  109.             if (cursor == null) {
  110.                 finish();
  111.                 return;
  112.             }
  113.             if (!cursor.moveToFirst()) {
  114.                 cursor.close();
  115.                 finish();
  116.                 return;
  117.             }
  118.             setTitle(R.string.sign_in);
  119.             providerId = cursor.getLong(ACCOUNT_PROVIDER_COLUMN);
  120.             provider = app.getProvider(providerId);
  121.             origUserName = cursor.getString(ACCOUNT_USERNAME_COLUMN);
  122.             mEditName.setText(origUserName);
  123.             mEditPass.setText(cursor.getString(ACCOUNT_PASSWORD_COLUMN));
  124.             mRememberPass.setChecked(!cursor.isNull(ACCOUNT_PASSWORD_COLUMN));
  125.             boolean keepSignIn = cursor.getInt(ACCOUNT_KEEP_SIGNED_IN_COLUMN) == 1;
  126.             mKeepSignIn.setChecked(keepSignIn);
  127.             cursor.close();
  128.         } else {
  129.             Log.w(ImApp.LOG_TAG, "<AccountActivity> unknown intent action " + action);
  130.             finish();
  131.             return;
  132.         }
  133.         final BrandingResources brandingRes = app.getBrandingResource(providerId);
  134.         mKeepSignIn.setOnClickListener(new OnClickListener() {
  135.             public void onClick(View v) {
  136.                 CheckBox keepSignIn = (CheckBox) v;
  137.                 if ( keepSignIn.isChecked() ) {
  138.                     String msg = brandingRes.getString(BrandingResourceIDs.STRING_TOAST_CHECK_AUTO_SIGN_IN);
  139.                     Toast.makeText(AccountActivity.this, msg, Toast.LENGTH_LONG).show();
  140.                 }
  141.             }
  142.         });
  143.         mRememberPass.setOnClickListener(new OnClickListener() {
  144.             public void onClick(View v) {
  145.                 CheckBox keepSignIn = (CheckBox) v;
  146.                 if ( keepSignIn.isChecked() ) {
  147.                     String msg = brandingRes.getString(BrandingResourceIDs.STRING_TOAST_CHECK_SAVE_PASSWORD);
  148.                     Toast.makeText(AccountActivity.this, msg, Toast.LENGTH_LONG).show();
  149.                 }
  150.             }
  151.         });
  152.         Drawable logo = brandingRes.getDrawable(BrandingResourceIDs.DRAWABLE_LOGO);
  153.         getWindow().setFeatureDrawable(Window.FEATURE_LEFT_ICON, logo);
  154.         TextView labelUsername = (TextView)findViewById(R.id.label_username);
  155.         labelUsername.setText(brandingRes.getString(BrandingResourceIDs.STRING_LABEL_USERNAME));
  156.         mEditName.addTextChangedListener(mTextWatcher);
  157.         mEditPass.addTextChangedListener(mTextWatcher);
  158.         mBtnSignIn.setOnClickListener(new OnClickListener() {
  159.             public void onClick(View v) {
  160.                 String username = mEditName.getText().toString();
  161.                 final String pass = mEditPass.getText().toString();
  162.                 final boolean rememberPass = mRememberPass.isChecked();
  163.                 ContentResolver cr = getContentResolver();
  164.                 long accountId = ImApp.insertOrUpdateAccount(cr, providerId, username,
  165.                         rememberPass ? pass : null);
  166.                 mAccountUri = ContentUris.withAppendedId(Im.Account.CONTENT_URI, accountId);
  167.                 if (!origUserName.equals(username) && shouldShowTermOfUse(brandingRes)) {
  168.                     comfirmTermsOfUse(brandingRes, new DialogInterface.OnClickListener() {
  169.                         public void onClick(DialogInterface dialog, int which) {
  170.                             signIn(rememberPass, pass);
  171.                         }
  172.                     });
  173.                 } else {
  174.                     signIn(rememberPass, pass);
  175.                 }
  176.             }
  177.             void signIn(boolean rememberPass, String pass) {
  178.                 Intent intent = new Intent(AccountActivity.this, SigningInActivity.class);
  179.                 intent.setData(mAccountUri);
  180.                 if (!rememberPass) {
  181.                     intent.putExtra(ImApp.EXTRA_INTENT_PASSWORD, pass);
  182.                 }
  183.                 if (mToAddress != null) {
  184.                     intent.putExtra(ImApp.EXTRA_INTENT_SEND_TO_USER, mToAddress);
  185.                 }
  186.                 startActivityForResult(intent, REQUEST_SIGN_IN);
  187.             }
  188.         });
  189.         // Make link for signing up.
  190.         String text = brandingRes.getString(BrandingResourceIDs.STRING_LABEL_SIGN_UP);
  191.         SpannableStringBuilder builder = new SpannableStringBuilder(text);
  192.         builder.setSpan(new URLSpan(provider.mSignUpUrl), 0, builder.length(),
  193.                 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  194.         TextView signUp = (TextView)findViewById(R.id.signUp);
  195.         signUp.setText(builder);
  196.         signUp.setMovementMethod(LinkMovementMethod.getInstance());
  197.         updateWidgetState();
  198.     }
  199.     void comfirmTermsOfUse(BrandingResources res, DialogInterface.OnClickListener accept) {
  200.         SpannableString message = new SpannableString(
  201.                 res.getString(BrandingResourceIDs.STRING_TOU_MESSAGE));
  202.         Linkify.addLinks(message, Linkify.ALL);
  203.         new AlertDialog.Builder(this)
  204.             .setIcon(android.R.drawable.ic_dialog_alert)
  205.             .setTitle(res.getString(BrandingResourceIDs.STRING_TOU_TITLE))
  206.             .setMessage(message)
  207.             .setPositiveButton(res.getString(BrandingResourceIDs.STRING_TOU_DECLINE), null)
  208.             .setNegativeButton(res.getString(BrandingResourceIDs.STRING_TOU_ACCEPT), accept)
  209.             .show();
  210.     }
  211.     boolean shouldShowTermOfUse(BrandingResources res) {
  212.         return !TextUtils.isEmpty(res.getString(BrandingResourceIDs.STRING_TOU_MESSAGE));
  213.     }
  214.     @Override
  215.     protected void onRestoreInstanceState(Bundle savedInstanceState) {
  216.         super.onRestoreInstanceState(savedInstanceState);
  217.         mAccountUri = savedInstanceState.getParcelable(ACCOUNT_URI_KEY);
  218.     }
  219.     @Override
  220.     protected void onSaveInstanceState(Bundle outState) {
  221.         super.onSaveInstanceState(outState);
  222.         outState.putParcelable(ACCOUNT_URI_KEY, mAccountUri);
  223.     }
  224.     @Override
  225.     protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  226.         if (requestCode == REQUEST_SIGN_IN) {
  227.             if (resultCode == RESULT_OK) {
  228.                 boolean keepSignIn = mKeepSignIn.isChecked();
  229.                 updateKeepSignedIn(keepSignIn);
  230.                 finish();
  231.             } else {
  232.                 // sign in failed, disable keep sign in, clear the password.
  233.                 mKeepSignIn.setChecked(false);
  234.                 updateKeepSignedIn(false);
  235.                 mEditPass.setText("");
  236.                 ContentValues values = new ContentValues();
  237.                 values.put(Im.Account.PASSWORD, (String) null);
  238.                 getContentResolver().update(mAccountUri, values, null, null);
  239.             }
  240.         }
  241.     }
  242.     void updateKeepSignedIn(boolean keepSignIn) {
  243.         ContentValues values = new ContentValues();
  244.         values.put(Im.Account.KEEP_SIGNED_IN, keepSignIn ? 1 : 0);
  245.         getContentResolver().update(mAccountUri, values, null, null);
  246.     }
  247.     void updateWidgetState() {
  248.         boolean goodUsername = mEditName.getText().length() > 0;
  249.         boolean goodPassword = mEditPass.getText().length() > 0;
  250.         boolean hasNameAndPassword = goodUsername && goodPassword;
  251.         mEditPass.setEnabled(goodUsername);
  252.         mEditPass.setFocusable(goodUsername);
  253.         mEditPass.setFocusableInTouchMode(goodUsername);
  254.         // enable keep sign in only when remember password is checked.
  255.         boolean rememberPass = mRememberPass.isChecked();
  256.         if (rememberPass && !hasNameAndPassword) {
  257.             mRememberPass.setChecked(false);
  258.             rememberPass = false;
  259.         }
  260.         mRememberPass.setEnabled(hasNameAndPassword);
  261.         mRememberPass.setFocusable(hasNameAndPassword);
  262.         if (!rememberPass) {
  263.             mKeepSignIn.setChecked(false);
  264.         }
  265.         mKeepSignIn.setEnabled(rememberPass);
  266.         mKeepSignIn.setFocusable(rememberPass);
  267.         mBtnSignIn.setEnabled(hasNameAndPassword);
  268.         mBtnSignIn.setFocusable(hasNameAndPassword);
  269.     }
  270.     private final TextWatcher mTextWatcher = new TextWatcher() {
  271.         public void beforeTextChanged(CharSequence s, int start, int before, int after) {
  272.         }
  273.         public void onTextChanged(CharSequence s, int start, int before, int after) {
  274.             updateWidgetState();
  275.         }
  276.         public void afterTextChanged(Editable s) {
  277.         }
  278.     };
  279. }