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