Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.calculator2; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.ObjectAnimator; |
| 21 | import android.app.Fragment; |
Annie Chin | 1ff328d | 2016-11-22 12:57:46 -0800 | [diff] [blame] | 22 | import android.app.FragmentTransaction; |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame] | 23 | import android.graphics.Color; |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 24 | import android.os.Bundle; |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame] | 25 | import android.support.v4.content.ContextCompat; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 26 | import android.support.v7.widget.RecyclerView; |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 27 | import android.view.LayoutInflater; |
| 28 | import android.view.MenuItem; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 29 | import android.view.MotionEvent; |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 30 | import android.view.View; |
| 31 | import android.view.ViewGroup; |
| 32 | import android.widget.Toolbar; |
| 33 | |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 34 | import java.util.ArrayList; |
| 35 | |
Christine Franks | 7485df5 | 2016-12-01 13:18:45 -0800 | [diff] [blame] | 36 | import static android.support.v7.widget.RecyclerView.SCROLL_STATE_DRAGGING; |
| 37 | |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 38 | public class HistoryFragment extends Fragment { |
| 39 | |
| 40 | public static final String TAG = "HistoryFragment"; |
Annie Chin | 532b77e | 2016-12-06 13:30:35 -0800 | [diff] [blame^] | 41 | public static final String CLEAR_DIALOG_TAG = "clear"; |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 42 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 43 | private final DragLayout.DragCallback mDragCallback = |
| 44 | new DragLayout.DragCallback() { |
| 45 | @Override |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 46 | public void onStartDraggingOpen() { |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 47 | // no-op |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public void whileDragging(float yFraction) { |
Annie Chin | 0e88baa | 2016-11-28 15:23:07 -0800 | [diff] [blame] | 52 | mDragController.animateViews(yFraction, mRecyclerView); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 56 | public boolean shouldInterceptTouchEvent(MotionEvent event) { |
Annie Chin | c5b6e4f | 2016-12-05 13:34:14 -0800 | [diff] [blame] | 57 | return !mRecyclerView.canScrollVertically(1); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public int getDisplayHeight() { |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void onLayout(int translation) { |
| 67 | // no-op |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | private final DragController mDragController = new DragController(); |
| 72 | |
| 73 | private RecyclerView mRecyclerView; |
| 74 | private HistoryAdapter mAdapter; |
Annie Chin | b2e9618 | 2016-11-28 13:14:54 -0800 | [diff] [blame] | 75 | private DragLayout mDragLayout; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 76 | |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 77 | private Evaluator mEvaluator; |
| 78 | |
| 79 | private ArrayList<HistoryItem> mDataSet = new ArrayList<>(); |
| 80 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 81 | @Override |
| 82 | public void onCreate(Bundle savedInstanceState) { |
| 83 | super.onCreate(savedInstanceState); |
| 84 | |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 85 | mAdapter = new HistoryAdapter((Calculator) getActivity(), mDataSet, |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 86 | getContext().getResources().getString(R.string.title_current_expression)); |
| 87 | } |
| 88 | |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 89 | @Override |
| 90 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 91 | Bundle savedInstanceState) { |
| 92 | final View view = inflater.inflate( |
| 93 | R.layout.fragment_history, container, false /* attachToRoot */); |
| 94 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 95 | mRecyclerView = (RecyclerView) view.findViewById(R.id.history_recycler_view); |
Christine Franks | 7485df5 | 2016-12-01 13:18:45 -0800 | [diff] [blame] | 96 | mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { |
| 97 | @Override |
| 98 | public void onScrollStateChanged(RecyclerView recyclerView, int newState) { |
| 99 | if (newState == SCROLL_STATE_DRAGGING) { |
| 100 | stopActionModeOrContextMenu(); |
| 101 | } |
| 102 | super.onScrollStateChanged(recyclerView, newState); |
| 103 | } |
| 104 | }); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 105 | |
| 106 | // The size of the RecyclerView is not affected by the adapter's contents. |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame] | 107 | mRecyclerView.setHasFixedSize(true); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 108 | mRecyclerView.setAdapter(mAdapter); |
| 109 | |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 110 | final Toolbar toolbar = (Toolbar) view.findViewById(R.id.history_toolbar); |
| 111 | toolbar.inflateMenu(R.menu.fragment_history); |
| 112 | toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { |
| 113 | @Override |
| 114 | public boolean onMenuItemClick(MenuItem item) { |
| 115 | if (item.getItemId() == R.id.menu_clear_history) { |
Annie Chin | 532b77e | 2016-12-06 13:30:35 -0800 | [diff] [blame^] | 116 | final Calculator calculator = (Calculator) getActivity(); |
| 117 | AlertDialogFragment.showMessageDialog(calculator, "" /* title */, |
| 118 | getString(R.string.dialog_clear), |
| 119 | getString(R.string.menu_clear_history), |
| 120 | CLEAR_DIALOG_TAG); |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 121 | return true; |
| 122 | } |
| 123 | return onOptionsItemSelected(item); |
| 124 | } |
| 125 | }); |
| 126 | toolbar.setNavigationOnClickListener(new View.OnClickListener() { |
| 127 | @Override |
| 128 | public void onClick(View v) { |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 129 | getActivity().onBackPressed(); |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 130 | } |
| 131 | }); |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 132 | |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 133 | return view; |
| 134 | } |
| 135 | |
| 136 | @Override |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 137 | public void onActivityCreated(Bundle savedInstanceState) { |
| 138 | super.onActivityCreated(savedInstanceState); |
| 139 | |
Annie Chin | 70ac8ea | 2016-11-18 14:43:56 -0800 | [diff] [blame] | 140 | final Calculator activity = (Calculator) getActivity(); |
Annie Chin | 94c1bd9 | 2016-11-23 13:39:56 -0800 | [diff] [blame] | 141 | final boolean isResultLayout = activity.isResultLayout(); |
Annie Chin | 70ac8ea | 2016-11-18 14:43:56 -0800 | [diff] [blame] | 142 | |
Annie Chin | b2e9618 | 2016-11-28 13:14:54 -0800 | [diff] [blame] | 143 | mDragLayout = (DragLayout) activity.findViewById(R.id.drag_layout); |
| 144 | mDragLayout.removeDragCallback(mDragCallback); |
| 145 | mDragLayout.addDragCallback(mDragCallback); |
| 146 | |
Annie Chin | 70ac8ea | 2016-11-18 14:43:56 -0800 | [diff] [blame] | 147 | mEvaluator = Evaluator.getInstance(activity); |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 148 | |
| 149 | if (mEvaluator != null) { |
Annie Chin | 94c1bd9 | 2016-11-23 13:39:56 -0800 | [diff] [blame] | 150 | initializeController(isResultLayout); |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 151 | |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 152 | final long maxIndex = mEvaluator.getMaxIndex(); |
| 153 | |
| 154 | final ArrayList<HistoryItem> newDataSet = new ArrayList<>(); |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 155 | |
Annie Chin | 94c1bd9 | 2016-11-23 13:39:56 -0800 | [diff] [blame] | 156 | if (!EvaluatorStateUtils.isDisplayEmpty(mEvaluator) && !isResultLayout) { |
Annie Chin | 70ac8ea | 2016-11-18 14:43:56 -0800 | [diff] [blame] | 157 | // Add the current expression as the first element in the list (the layout is |
| 158 | // reversed and we want the current expression to be the last one in the |
| 159 | // recyclerview). |
| 160 | // If we are in the result state, the result will animate to the last history |
| 161 | // element in the list and there will be no "Current Expression." |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 162 | mEvaluator.copyMainToHistory(); |
| 163 | newDataSet.add(new HistoryItem(Evaluator.HISTORY_MAIN_INDEX, |
| 164 | System.currentTimeMillis(), mEvaluator.getExprAsSpannable(0))); |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 165 | } |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame] | 166 | for (long i = 0; i < maxIndex; ++i) { |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 167 | newDataSet.add(null); |
| 168 | } |
Annie Chin | 3614798 | 2016-12-01 15:07:34 -0800 | [diff] [blame] | 169 | final boolean isEmpty = newDataSet.isEmpty(); |
| 170 | mRecyclerView.setBackgroundColor(isEmpty |
| 171 | ? ContextCompat.getColor(activity, R.color.empty_history_color) |
| 172 | : Color.TRANSPARENT); |
| 173 | if (isEmpty) { |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 174 | newDataSet.add(new HistoryItem()); |
| 175 | } |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 176 | mDataSet = newDataSet; |
| 177 | mAdapter.setDataSet(mDataSet); |
Annie Chin | 94c1bd9 | 2016-11-23 13:39:56 -0800 | [diff] [blame] | 178 | mAdapter.setIsResultLayout(isResultLayout); |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 179 | } |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 180 | |
| 181 | mAdapter.notifyDataSetChanged(); |
Annie Chin | efa259a | 2016-11-23 14:15:15 -0800 | [diff] [blame] | 182 | } |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 183 | |
Annie Chin | efa259a | 2016-11-23 14:15:15 -0800 | [diff] [blame] | 184 | @Override |
| 185 | public void onStart() { |
| 186 | super.onStart(); |
| 187 | |
| 188 | // The orientation may have changed. |
Annie Chin | 94c1bd9 | 2016-11-23 13:39:56 -0800 | [diff] [blame] | 189 | mDragController.initializeAnimation(mRecyclerView, |
Annie Chin | b2e9618 | 2016-11-28 13:14:54 -0800 | [diff] [blame] | 190 | ((Calculator) getActivity()).isResultLayout(), mDragLayout.isOpen()); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 194 | public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) { |
| 195 | final View view = getView(); |
| 196 | final int height = getResources().getDisplayMetrics().heightPixels; |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame] | 197 | if (enter) { |
Annie Chin | 1ff328d | 2016-11-22 12:57:46 -0800 | [diff] [blame] | 198 | if (transit == FragmentTransaction.TRANSIT_FRAGMENT_OPEN) { |
| 199 | return ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, -height, 0f); |
| 200 | } else { |
| 201 | return null; |
| 202 | } |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame] | 203 | } else { |
| 204 | return ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, -height); |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 205 | } |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 208 | @Override |
| 209 | public void onDestroyView() { |
| 210 | final DragLayout dragLayout = (DragLayout) getActivity().findViewById(R.id.drag_layout); |
| 211 | if (dragLayout != null) { |
| 212 | dragLayout.removeDragCallback(mDragCallback); |
| 213 | } |
Annie Chin | 06fd3cf | 2016-11-07 16:04:33 -0800 | [diff] [blame] | 214 | |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 215 | // Note that the view is destroyed when the fragment backstack is popped, so |
| 216 | // these are essentially called when the DragLayout is closed. |
Hans Boehm | 31ea252 | 2016-11-23 17:47:02 -0800 | [diff] [blame] | 217 | mEvaluator.cancelNonMain(); |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 218 | |
| 219 | super.onDestroyView(); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Annie Chin | 94c1bd9 | 2016-11-23 13:39:56 -0800 | [diff] [blame] | 222 | private void initializeController(boolean isResult) { |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 223 | mDragController.setDisplayFormula( |
| 224 | (CalculatorFormula) getActivity().findViewById(R.id.formula)); |
| 225 | |
| 226 | mDragController.setDisplayResult( |
| 227 | (CalculatorResult) getActivity().findViewById(R.id.result)); |
| 228 | |
| 229 | mDragController.setToolbar(getActivity().findViewById(R.id.toolbar)); |
Annie Chin | bc00188 | 2016-11-09 19:41:21 -0800 | [diff] [blame] | 230 | |
| 231 | mDragController.setEvaluator(mEvaluator); |
Annie Chin | 94c1bd9 | 2016-11-23 13:39:56 -0800 | [diff] [blame] | 232 | |
| 233 | mDragController.initializeController(isResult); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Christine Franks | 7485df5 | 2016-12-01 13:18:45 -0800 | [diff] [blame] | 236 | public boolean stopActionModeOrContextMenu() { |
Annie Chin | 1fcfc31 | 2016-12-06 09:36:36 -0800 | [diff] [blame] | 237 | if (mRecyclerView == null) { |
| 238 | return false; |
| 239 | } |
Christine Franks | 7485df5 | 2016-12-01 13:18:45 -0800 | [diff] [blame] | 240 | for (int i = 0; i < mRecyclerView.getChildCount(); i++) { |
| 241 | final View view = mRecyclerView.getChildAt(i); |
| 242 | final HistoryAdapter.ViewHolder viewHolder = |
| 243 | (HistoryAdapter.ViewHolder) mRecyclerView.getChildViewHolder(view); |
Annie Chin | 532b77e | 2016-12-06 13:30:35 -0800 | [diff] [blame^] | 244 | if (viewHolder != null && viewHolder.getResult() != null |
| 245 | && viewHolder.getResult().stopActionModeOrContextMenu()) { |
Christine Franks | 7485df5 | 2016-12-01 13:18:45 -0800 | [diff] [blame] | 246 | return true; |
| 247 | } |
| 248 | } |
| 249 | return false; |
| 250 | } |
Annie Chin | abd202f | 2016-10-14 14:23:45 -0700 | [diff] [blame] | 251 | } |