1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.transitiontests; 17 18 import android.app.Activity; 19 import android.os.Bundle; 20 import android.view.View; 21 import android.view.ViewGroup; 22 import android.transition.Scene; 23 import android.widget.TextView; 24 import android.transition.Fade; 25 import android.transition.Recolor; 26 import android.transition.Slide; 27 import android.transition.Transition; 28 import android.transition.TransitionSet; 29 import android.transition.TransitionManager; 30 31 32 public class LoginActivity extends Activity { 33 ViewGroup mSceneRoot; 34 Scene mCurrentScene; 35 TransitionManager mTransitionManager; 36 Scene mLoginScene, mPasswordScene, mIncorrectPasswordScene, mSuccessScene, mUsernameTakenScene, 37 mNewUserScene; 38 39 @Override onCreate(Bundle savedInstanceState)40 public void onCreate(Bundle savedInstanceState) { 41 super.onCreate(savedInstanceState); 42 setContentView(R.layout.activity_login); 43 View container = findViewById(R.id.container); 44 mSceneRoot = (ViewGroup) container.getParent(); 45 46 mLoginScene = Scene.getSceneForLayout(mSceneRoot, R.layout.activity_login, this); 47 mPasswordScene = Scene.getSceneForLayout(mSceneRoot, R.layout.login_password, this); 48 mIncorrectPasswordScene = Scene.getSceneForLayout(mSceneRoot, R.layout.incorrect_password, this); 49 mUsernameTakenScene = Scene.getSceneForLayout(mSceneRoot, R.layout.username_taken, this); 50 mSuccessScene = Scene.getSceneForLayout(mSceneRoot, R.layout.success, this); 51 mNewUserScene = Scene.getSceneForLayout(mSceneRoot, R.layout.new_user, this); 52 53 mTransitionManager = new TransitionManager(); 54 55 // Custom transitions in/out of NewUser screen - slide in the 2nd password UI 56 TransitionSet slider = new TransitionSet(); 57 slider.addTransition(new Slide().addTarget(R.id.retype).addTarget(R.id.retypeEdit)); 58 slider.addTransition(new Recolor().addTarget(R.id.password). 59 addTarget(R.id.passwordEdit)); 60 slider.addTransition(new Fade()); 61 mTransitionManager.setTransition(mLoginScene, mNewUserScene, slider); 62 mTransitionManager.setTransition(mPasswordScene, mNewUserScene, slider); 63 mTransitionManager.setTransition(mNewUserScene, mLoginScene, slider); 64 mTransitionManager.setTransition(mNewUserScene, mPasswordScene, slider); 65 66 // Custom transitions with recoloring password field 67 Transition colorizer = new Recolor().addTarget(R.id.password). 68 addTarget(R.id.passwordEdit); 69 mTransitionManager.setTransition(mLoginScene, mPasswordScene, colorizer); 70 mTransitionManager.setTransition(mPasswordScene, mLoginScene, colorizer); 71 72 mCurrentScene = mLoginScene; 73 } 74 applyScene(Scene scene)75 public void applyScene(Scene scene) { 76 mTransitionManager.transitionTo(scene); 77 mCurrentScene = scene; 78 } 79 sendMessage(View view)80 public void sendMessage(View view) { 81 TextView textView = (TextView) view; 82 CharSequence text = textView.getText(); 83 if (text.equals("Cancel")) { 84 applyScene(mLoginScene); 85 } else if (text.equals("Submit")) { 86 if (mCurrentScene == mLoginScene) { 87 applyScene(mPasswordScene); 88 } else if (mCurrentScene == mPasswordScene) { 89 applyScene(Math.random() < .5 ? mSuccessScene : mIncorrectPasswordScene); 90 } else if (mCurrentScene == mNewUserScene) { 91 applyScene(Math.random() < .5 ? mSuccessScene : mUsernameTakenScene); 92 } 93 } else if (text.equals("New User?")) { 94 applyScene(mNewUserScene); 95 } else if (text.equals("Okay")) { 96 if (mCurrentScene == mIncorrectPasswordScene) { 97 applyScene(mPasswordScene); 98 } else { // username taken scene 99 applyScene(mNewUserScene); 100 } 101 } else if (text.equals("Reset")) { 102 applyScene(mLoginScene); 103 } 104 } 105 } 106