1 /*
2  * Copyright (C) 2014 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 
17 package com.example.android.activityscenetransitionbasic;
18 
19 import com.squareup.picasso.Picasso;
20 
21 import android.app.Activity;
22 import android.os.Build;
23 import android.os.Bundle;
24 import android.support.v4.view.ViewCompat;
25 import android.transition.Transition;
26 import android.widget.ImageView;
27 import android.widget.TextView;
28 
29 /**
30  * Our secondary Activity which is launched from {@link MainActivity}. Has a simple detail UI
31  * which has a large banner image, title and body text.
32  */
33 public class DetailActivity extends Activity {
34 
35     // Extra name for the ID parameter
36     public static final String EXTRA_PARAM_ID = "detail:_id";
37 
38     // View name of the header image. Used for activity scene transitions
39     public static final String VIEW_NAME_HEADER_IMAGE = "detail:header:image";
40 
41     // View name of the header title. Used for activity scene transitions
42     public static final String VIEW_NAME_HEADER_TITLE = "detail:header:title";
43 
44     private ImageView mHeaderImageView;
45     private TextView mHeaderTitle;
46 
47     private Item mItem;
48 
49     @Override
onCreate(Bundle savedInstanceState)50     protected void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52         setContentView(R.layout.details);
53 
54         // Retrieve the correct Item instance, using the ID provided in the Intent
55         mItem = Item.getItem(getIntent().getIntExtra(EXTRA_PARAM_ID, 0));
56 
57         mHeaderImageView = (ImageView) findViewById(R.id.imageview_header);
58         mHeaderTitle = (TextView) findViewById(R.id.textview_title);
59 
60         // BEGIN_INCLUDE(detail_set_view_name)
61         /**
62          * Set the name of the view's which will be transition to, using the static values above.
63          * This could be done in the layout XML, but exposing it via static variables allows easy
64          * querying from other Activities
65          */
66         ViewCompat.setTransitionName(mHeaderImageView, VIEW_NAME_HEADER_IMAGE);
67         ViewCompat.setTransitionName(mHeaderTitle, VIEW_NAME_HEADER_TITLE);
68         // END_INCLUDE(detail_set_view_name)
69 
70         loadItem();
71     }
72 
loadItem()73     private void loadItem() {
74         // Set the title TextView to the item's name and author
75         mHeaderTitle.setText(getString(R.string.image_header, mItem.getName(), mItem.getAuthor()));
76 
77         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && addTransitionListener()) {
78             // If we're running on Lollipop and we have added a listener to the shared element
79             // transition, load the thumbnail. The listener will load the full-size image when
80             // the transition is complete.
81             loadThumbnail();
82         } else {
83             // If all other cases we should just load the full-size image now
84             loadFullSizeImage();
85         }
86     }
87 
88     /**
89      * Load the item's thumbnail image into our {@link ImageView}.
90      */
loadThumbnail()91     private void loadThumbnail() {
92         Picasso.with(mHeaderImageView.getContext())
93                 .load(mItem.getThumbnailUrl())
94                 .noFade()
95                 .into(mHeaderImageView);
96     }
97 
98     /**
99      * Load the item's full-size image into our {@link ImageView}.
100      */
loadFullSizeImage()101     private void loadFullSizeImage() {
102         Picasso.with(mHeaderImageView.getContext())
103                 .load(mItem.getPhotoUrl())
104                 .noFade()
105                 .noPlaceholder()
106                 .into(mHeaderImageView);
107     }
108 
109     /**
110      * Try and add a {@link Transition.TransitionListener} to the entering shared element
111      * {@link Transition}. We do this so that we can load the full-size image after the transition
112      * has completed.
113      *
114      * @return true if we were successful in adding a listener to the enter transition
115      */
addTransitionListener()116     private boolean addTransitionListener() {
117         final Transition transition = getWindow().getSharedElementEnterTransition();
118 
119         if (transition != null) {
120             // There is an entering shared element transition so add a listener to it
121             transition.addListener(new Transition.TransitionListener() {
122                 @Override
123                 public void onTransitionEnd(Transition transition) {
124                     // As the transition has ended, we can now load the full-size image
125                     loadFullSizeImage();
126 
127                     // Make sure we remove ourselves as a listener
128                     transition.removeListener(this);
129                 }
130 
131                 @Override
132                 public void onTransitionStart(Transition transition) {
133                     // No-op
134                 }
135 
136                 @Override
137                 public void onTransitionCancel(Transition transition) {
138                     // Make sure we remove ourselves as a listener
139                     transition.removeListener(this);
140                 }
141 
142                 @Override
143                 public void onTransitionPause(Transition transition) {
144                     // No-op
145                 }
146 
147                 @Override
148                 public void onTransitionResume(Transition transition) {
149                     // No-op
150                 }
151             });
152             return true;
153         }
154 
155         // If we reach here then we have not added a listener
156         return false;
157     }
158 
159 }
160