1 /*
2 * Copyright 2013 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.batchstepsensor;
18 
19 import android.os.Bundle;
20 import android.support.v4.app.FragmentManager;
21 import android.support.v4.app.FragmentTransaction;
22 import android.view.Menu;
23 
24 import com.example.android.common.activities.SampleActivityBase;
25 import com.example.android.common.logger.Log;
26 
27 import com.example.android.batchstepsensor.cardstream.CardStream;
28 import com.example.android.batchstepsensor.cardstream.CardStreamFragment;
29 import com.example.android.batchstepsensor.cardstream.CardStreamState;
30 import com.example.android.batchstepsensor.cardstream.OnCardClickListener;
31 import com.example.android.batchstepsensor.cardstream.StreamRetentionFragment;
32 
33 public class MainActivity extends SampleActivityBase implements CardStream {
34     public static final String TAG = "MainActivity";
35     public static final String FRAGTAG = "BatchStepSensorFragment";
36 
37     private CardStreamFragment mCardStreamFragment;
38 
39     private StreamRetentionFragment mRetentionFragment;
40     private static final String RETENTION_TAG = "retention";
41 
42     @Override
onCreate(Bundle savedInstanceState)43     protected void onCreate(Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45         setContentView(R.layout.activity_main);
46 
47         FragmentManager fm = getSupportFragmentManager();
48         BatchStepSensorFragment fragment =
49                 (BatchStepSensorFragment) fm.findFragmentByTag(FRAGTAG);
50 
51         if (fragment == null) {
52             FragmentTransaction transaction = fm.beginTransaction();
53             fragment = new BatchStepSensorFragment();
54             transaction.add(fragment, FRAGTAG);
55             transaction.commit();
56         }
57 
58         // Use fragment as click listener for cards, but must implement correct interface
59         if (!(fragment instanceof OnCardClickListener)){
60             throw new ClassCastException("BatchStepSensorFragment must " +
61                     "implement OnCardClickListener interface.");
62         }
63         OnCardClickListener clickListener = (OnCardClickListener) fm.findFragmentByTag(FRAGTAG);
64 
65         mRetentionFragment = (StreamRetentionFragment) fm.findFragmentByTag(RETENTION_TAG);
66         if (mRetentionFragment == null) {
67             mRetentionFragment = new StreamRetentionFragment();
68             fm.beginTransaction().add(mRetentionFragment, RETENTION_TAG).commit();
69         } else {
70             // If the retention fragment already existed, we need to pull some state.
71             // pull state out
72             CardStreamState state = mRetentionFragment.getCardStream();
73 
74             // dump it in CardStreamFragment.
75             mCardStreamFragment =
76                     (CardStreamFragment) fm.findFragmentById(R.id.fragment_cardstream);
77             mCardStreamFragment.restoreState(state, clickListener);
78         }
79     }
80 
getCardStream()81     public CardStreamFragment getCardStream() {
82         if (mCardStreamFragment == null) {
83             mCardStreamFragment = (CardStreamFragment)
84                     getSupportFragmentManager().findFragmentById(R.id.fragment_cardstream);
85         }
86         return mCardStreamFragment;
87     }
88 
89     @Override
onSaveInstanceState(Bundle outState)90     protected void onSaveInstanceState(Bundle outState) {
91         super.onSaveInstanceState(outState);
92         CardStreamState state = getCardStream().dumpState();
93         mRetentionFragment.storeCardStream(state);
94     }
95 }
96