content: /*
   * Copyright (C) 2014 The Android Open Source Project
   *
   * 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
   *
   *      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.example.android.activityscenetransitionbasic;
  
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.BaseAdapter;
  import android.widget.GridView;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.core.app.ActivityCompat;
  import androidx.core.app.ActivityOptionsCompat;
  import androidx.core.util.Pair;
  
  import com.squareup.picasso.Picasso;
  
  /**
   * Our main Activity in this sample. Displays a grid of items which an image and title. When the
   * user clicks on an item, {@link DetailActivity} is launched, using the Activity Scene Transitions
   * framework to animatedly do so.
   */
  public class MainActivity extends AppCompatActivity {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.grid);
  
          // Setup the GridView and set the adapter
          GridView grid = findViewById(R.id.grid);
          grid.setOnItemClickListener(mOnItemClickListener);
          GridAdapter adapter = new GridAdapter();
          grid.setAdapter(adapter);
      }
  
      private final AdapterView.OnItemClickListener mOnItemClickListener
              = new AdapterView.OnItemClickListener() {
  
          /**
           * Called when an item in the {@link android.widget.GridView} is clicked. Here will launch
           * the {@link DetailActivity}, using the Scene Transition animation functionality.
           */
          @Override
          public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
              Item item = (Item) adapterView.getItemAtPosition(position);
  
              // Construct an Intent as normal
              Intent intent = new Intent(MainActivity.this, DetailActivity.class);
              intent.putExtra(DetailActivity.EXTRA_PARAM_ID, item.getId());
  
              // BEGIN_INCLUDE(start_activity)
              /*
               * Now create an {@link android.app.ActivityOptions} instance using the
               * {@link ActivityOptionsCompat#makeSceneTransitionAnimation(Activity, Pair[])} factory
               * method.
               */
              @SuppressWarnings("unchecked")
              ActivityOptionsCompat activityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(
                      MainActivity.this,
  
                      // Now we provide a list of Pair items which contain the view we can transitioning
                      // from, and the name of the view it is transitioning to, in the launched activity
                      new Pair<>(view.findViewById(R.id.imageview_item),
                              DetailActivity.VIEW_NAME_HEADER_IMAGE),
                      new Pair<>(view.findViewById(R.id.textview_name),
                              DetailActivity.VIEW_NAME_HEADER_TITLE));
  
              // Now we can start the Activity, providing the activity options as a bundle
              ActivityCompat.startActivity(MainActivity.this, intent, activityOptions.toBundle());
              // END_INCLUDE(start_activity)
          }
      };
  
      /**
       * {@link android.widget.BaseAdapter} which displays items.
       */
      private class GridAdapter extends BaseAdapter {
  
          @Override
          public int getCount() {
              return Item.ITEMS.length;
          }
  
          @Override
          public Item getItem(int position) {
              return Item.ITEMS[position];
          }
  
          @Override
          public long getItemId(int position) {
              return getItem(position).getId();
          }
  
          @Override
          public View getView(int position, View view, ViewGroup viewGroup) {
              if (view == null) {
                  view = getLayoutInflater().inflate(R.layout.grid_item, viewGroup, false);
              }
  
              final Item item = getItem(position);
  
              // Load the thumbnail image
              ImageView image = view.findViewById(R.id.imageview_item);
              Picasso.with(image.getContext()).load(item.getThumbnailUrl()).into(image);
  
              // Set the TextView's contents
              TextView name = view.findViewById(R.id.textview_name);
              name.setText(item.getName());
  
              return view;
          }
      }
  }
  