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