1 /*
2  * Copyright (C) 2015 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.test.uibench;
17 
18 import android.app.ActivityOptions;
19 import android.app.SharedElementCallback;
20 import android.content.Intent;
21 import android.content.res.Configuration;
22 import android.graphics.Color;
23 import android.graphics.drawable.ColorDrawable;
24 import android.os.Bundle;
25 import androidx.appcompat.app.AppCompatActivity;
26 import android.view.View;
27 import android.widget.GridLayout;
28 import android.widget.ImageView;
29 
30 import java.util.List;
31 import java.util.Map;
32 
33 public class ActivityTransition extends AppCompatActivity {
34     private static final String KEY_ID = "ViewTransitionValues:id";
35 
36     private ImageView mHero;
37 
38     public static final int[] DRAWABLES = {
39             R.drawable.ball,
40             R.drawable.block,
41             R.drawable.ducky,
42             R.drawable.jellies,
43             R.drawable.mug,
44             R.drawable.pencil,
45             R.drawable.scissors,
46             R.drawable.woot,
47     };
48 
49     public static final int[] IDS = {
50             R.id.ball,
51             R.id.block,
52             R.id.ducky,
53             R.id.jellies,
54             R.id.mug,
55             R.id.pencil,
56             R.id.scissors,
57             R.id.woot,
58     };
59 
60     public static final String[] NAMES = {
61             "ball",
62             "block",
63             "ducky",
64             "jellies",
65             "mug",
66             "pencil",
67             "scissors",
68             "woot",
69     };
70 
getIdForKey(String id)71     public static int getIdForKey(String id) {
72         return IDS[getIndexForKey(id)];
73     }
74 
getDrawableIdForKey(String id)75     public static int getDrawableIdForKey(String id) {
76         return DRAWABLES[getIndexForKey(id)];
77     }
78 
getIndexForKey(String id)79     public static int getIndexForKey(String id) {
80         for (int i = 0; i < NAMES.length; i++) {
81             String name = NAMES[i];
82             if (name.equals(id)) {
83                 return i;
84             }
85         }
86         return 2;
87     }
88 
89     @Override
onCreate(Bundle savedInstanceState)90     protected void onCreate(Bundle savedInstanceState) {
91         super.onCreate(savedInstanceState);
92         getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLACK));
93         setContentView(R.layout.activity_transition);
94         setupHero();
95 
96         // Ensure that all images are visible regardless of orientation.
97         GridLayout gridLayout = findViewById(R.id.transition_grid_layout);
98         boolean isPortrait =
99                 getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
100         gridLayout.setRowCount(isPortrait ? 4 : 2);
101         gridLayout.setColumnCount(isPortrait ? 2 : 4);
102     }
103 
setupHero()104     private void setupHero() {
105         String name = getIntent().getStringExtra(KEY_ID);
106         mHero = null;
107         if (name != null) {
108             mHero = findViewById(getIdForKey(name));
109             setEnterSharedElementCallback(new SharedElementCallback() {
110                 @Override
111                 public void onMapSharedElements(List<String> names,
112                         Map<String, View> sharedElements) {
113                     sharedElements.put("hero", mHero);
114                 }
115             });
116         }
117     }
118 
clicked(View v)119     public void clicked(View v) {
120         mHero = (ImageView) v;
121         Intent intent = new Intent(this, ActivityTransitionDetails.class);
122         intent.putExtra(KEY_ID, v.getTransitionName());
123         ActivityOptions activityOptions
124                 = ActivityOptions.makeSceneTransitionAnimation(this, mHero, "hero");
125         startActivity(intent, activityOptions.toBundle());
126     }
127 }
128