1 /* 2 * Copyright (C) 2017 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.android.incallui.incall.impl; 18 19 import android.support.annotation.Nullable; 20 import android.support.v4.app.Fragment; 21 import android.support.v4.app.FragmentManager; 22 import android.support.v4.app.FragmentStatePagerAdapter; 23 import android.support.v4.view.PagerAdapter; 24 import com.android.dialer.common.Assert; 25 import com.android.dialer.multimedia.MultimediaData; 26 import com.android.incallui.sessiondata.MultimediaFragment; 27 28 /** View pager adapter for in call ui. */ 29 public class InCallPagerAdapter extends FragmentStatePagerAdapter { 30 31 @Nullable private MultimediaData attachments; 32 private final boolean showInCallButtonGrid; 33 InCallPagerAdapter( FragmentManager fragmentManager, @Nullable MultimediaData attachments, boolean showInCallButtonGrid)34 public InCallPagerAdapter( 35 FragmentManager fragmentManager, 36 @Nullable MultimediaData attachments, 37 boolean showInCallButtonGrid) { 38 super(fragmentManager); 39 this.attachments = attachments; 40 this.showInCallButtonGrid = showInCallButtonGrid; 41 } 42 43 @Override getItem(int position)44 public Fragment getItem(int position) { 45 if (!showInCallButtonGrid) { 46 // TODO(calderwoodra): handle fragment invalidation for when the data changes. 47 return MultimediaFragment.newInstance( 48 attachments, true /* isInteractive */, false /* showAvatar */, false /* isSpam */); 49 50 } else if (position == getButtonGridPosition()) { 51 return InCallButtonGridFragment.newInstance(); 52 53 } else { 54 return MultimediaFragment.newInstance( 55 attachments, true /* isInteractive */, false /* showAvatar */, false /* isSpam */); 56 } 57 } 58 59 @Override getCount()60 public int getCount() { 61 int count = 0; 62 if (showInCallButtonGrid) { 63 count++; 64 } 65 if (attachments != null && attachments.hasData()) { 66 count++; 67 } 68 Assert.checkArgument(count > 0, "InCallPager adapter doesn't have any pages."); 69 return count; 70 } 71 setAttachments(@ullable MultimediaData attachments)72 public void setAttachments(@Nullable MultimediaData attachments) { 73 if (this.attachments != attachments) { 74 this.attachments = attachments; 75 notifyDataSetChanged(); 76 } 77 } 78 getButtonGridPosition()79 public int getButtonGridPosition() { 80 return getCount() - 1; 81 } 82 83 //this is called when notifyDataSetChanged() is called 84 @Override getItemPosition(Object object)85 public int getItemPosition(Object object) { 86 // refresh all fragments when data set changed 87 return PagerAdapter.POSITION_NONE; 88 } 89 } 90