Advertisement
moonlightcheese

Untitled

Jan 10th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.56 KB | None | 0 0
  1. package com.conceptualsystems.smsmobile2;
  2.  
  3. //import android.app.Activity;
  4. import android.content.res.Configuration;
  5. import android.support.v4.app.FragmentActivity;
  6. import android.os.Bundle;
  7. import android.support.v4.app.FragmentManager;
  8. import android.support.v4.app.FragmentTransaction;
  9. import android.util.Log;
  10. import android.widget.LinearLayout;
  11.  
  12. public class MainActivity extends FragmentActivity implements SmsTicketListFragment.OnTicketSelectedListener
  13. {
  14.     private final FragmentManager mFM = getSupportFragmentManager();
  15.     private Configuration mConfig;
  16.     LinearLayout mTicketListLayout = null;
  17.     LinearLayout mTicketReviewLayout = null;
  18.     SmsTicketListFragment mTicketListFragment = null;
  19.     SmsTicketReviewFragment mTicketReviewFragment = null;
  20.     public static int TICKET_LIST_LAYOUT_ID = 1;
  21.     public static int TICKET_REVIEW_LAYOUT_ID = 2;
  22.    
  23.     public void onTicketSelected(String selection) {
  24.         mTicketReviewFragment = new SmsTicketReviewFragment(selection);  //fm.findFragmentById(R.id.ticket_review_fragment);
  25.         FragmentTransaction transaction = mFM.beginTransaction();
  26.  
  27.         if((mConfig.screenLayout&Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
  28.             transaction.replace(TICKET_REVIEW_LAYOUT_ID, mTicketReviewFragment);
  29.         } else if((mConfig.screenLayout&Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
  30.             transaction.replace(TICKET_LIST_LAYOUT_ID, mTicketReviewFragment);
  31.             transaction.addToBackStack(null);
  32.         }
  33.  
  34.         transaction.commit();
  35.        
  36.         //Log.i("SMSTICKET", selection);
  37.     }
  38.  
  39.     /** Called when the activity is first created. */
  40.     @Override
  41.     public void onCreate(Bundle savedInstanceState)
  42.     {
  43.         super.onCreate(savedInstanceState);
  44.         Log.i("TAG", "onCreate called");
  45.         setContentView(R.layout.main);
  46.         Log.i("TAG", getResources().getDisplayMetrics().toString());
  47.         mConfig = getResources().getConfiguration();
  48.         initScreen();
  49.     }
  50.  
  51.     public void onStart() {
  52.         super.onStart();
  53.         Log.i("TAG", "onStart called");
  54.     }
  55.  
  56.     public void onResume() {
  57.         super.onResume();
  58.         Log.i("TAG", "onResume called");
  59.     }
  60.  
  61.     public void onPause() {
  62.         super.onPause();
  63.         Log.i("TAG", "onPause called");
  64.     }
  65.  
  66.     public void onStop() {
  67.         super.onStart();
  68.         Log.i("TAG", "onStop called");
  69.     }
  70.  
  71.     public void onConfigurationChanged(Configuration newConfig) {
  72.         super.onConfigurationChanged(newConfig);
  73.         mConfig = newConfig;
  74.         initScreen();
  75.     }
  76.  
  77.     public void initScreen() {
  78.         switch(mConfig.screenLayout&Configuration.SCREENLAYOUT_SIZE_MASK) {
  79.             case Configuration.SCREENLAYOUT_SIZE_SMALL:
  80.             {
  81.                 Log.i("TAG", "Android says: small");
  82.             }
  83.             case Configuration.SCREENLAYOUT_SIZE_NORMAL:
  84.             {
  85.                 Log.i("TAG", "Android says: normal / small");
  86.                 mTicketListLayout = new LinearLayout(this);
  87.                 mTicketListLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
  88.                 mTicketListLayout.setId(TICKET_LIST_LAYOUT_ID);
  89.  
  90.                 FragmentTransaction transaction = mFM.beginTransaction();
  91.                 boolean changed = false;
  92.                 if(mFM.findFragmentByTag("ticket_list_fragment") == null) {
  93.                     mTicketListFragment = new SmsTicketListFragment();
  94.                     transaction.add(TICKET_LIST_LAYOUT_ID, mTicketListFragment, "ticket_list_fragment");
  95.                     changed = true;
  96.                 }
  97.                 if(changed)
  98.                     transaction.commit();
  99.  
  100.                 if(findViewById(TICKET_LIST_LAYOUT_ID) == null)
  101.                     ((LinearLayout)findViewById(R.id.root_view)).addView(mTicketListLayout);
  102.  
  103.                 break;
  104.             }
  105.             case Configuration.SCREENLAYOUT_SIZE_LARGE:
  106.             {
  107.                 Log.i("TAG", "Android says: large");
  108.                 mTicketListLayout = new LinearLayout(this);
  109.                 mTicketListLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
  110.                 mTicketListLayout.setId(TICKET_LIST_LAYOUT_ID);
  111.  
  112.                 mTicketReviewLayout = new LinearLayout(this);
  113.                 mTicketReviewLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
  114.                 mTicketReviewLayout.setId(TICKET_REVIEW_LAYOUT_ID);
  115.  
  116.                 FragmentTransaction transaction = mFM.beginTransaction();
  117.                 boolean changed=false;
  118.                 if(mFM.findFragmentByTag("ticket_list_fragment") == null) {
  119.                     mTicketListFragment = new SmsTicketListFragment();
  120.                     transaction.add(TICKET_LIST_LAYOUT_ID, mTicketListFragment, "ticket_list_fragment");
  121.                     changed = true;
  122.                 }
  123.                 if(mFM.findFragmentByTag("ticket_review_fragment") == null) {
  124.                     mTicketReviewFragment = new SmsTicketReviewFragment();
  125.                     transaction.add(TICKET_REVIEW_LAYOUT_ID, mTicketReviewFragment, "ticket_review_fragment");
  126.                     changed = true;
  127.                 }
  128.                 if(changed)
  129.                     transaction.commit();
  130.  
  131.                 if(mConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
  132.                     ((LinearLayout)findViewById(R.id.root_view)).setOrientation(LinearLayout.HORIZONTAL);
  133.                 } else if(mConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
  134.                     ((LinearLayout)findViewById(R.id.root_view)).setOrientation(LinearLayout.VERTICAL);
  135.                 }
  136.  
  137.                 if(findViewById(TICKET_LIST_LAYOUT_ID) == null)
  138.                     ((LinearLayout)findViewById(R.id.root_view)).addView(mTicketListLayout);
  139.                 if(findViewById(TICKET_REVIEW_LAYOUT_ID) == null)
  140.                     ((LinearLayout)findViewById(R.id.root_view)).addView(mTicketReviewLayout);
  141.                
  142.                 break;
  143.             }
  144.             /***** not supported?  *****
  145.             case Configuration.SCREENLAYOUT_SIZE_XLARGE:
  146.                 Log.i("TAG", "Android says: xlarge");
  147.                 break;
  148.             */
  149.         }
  150.  
  151.         //set the screen according to screen layout and orientation
  152.        
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement