Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2007 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.apis.app;
     18 
     19 import com.example.android.apis.R;
     20 
     21 import android.app.Activity;
     22 import android.content.Intent;
     23 import android.content.SharedPreferences;
     24 import android.os.Bundle;
     25 import android.view.View;
     26 import android.view.View.OnClickListener;
     27 import android.widget.Button;
     28 import android.widget.TextView;
     29 
     30 /**
     31  * Entry into our redirection example, describing what will happen.
     32  */
     33 public class RedirectMain extends Activity {
     34     static final int INIT_TEXT_REQUEST = 0;
     35     static final int NEW_TEXT_REQUEST = 1;
     36 
     37     @Override
     38 	protected void onCreate(Bundle savedInstanceState) {
     39         super.onCreate(savedInstanceState);
     40 
     41         setContentView(R.layout.redirect_main);
     42 
     43         // Watch for button clicks.
     44         Button clearButton = (Button)findViewById(R.id.clear);
     45         clearButton.setOnClickListener(mClearListener);
     46         Button newButton = (Button)findViewById(R.id.newView);
     47         newButton.setOnClickListener(mNewListener);
     48 
     49         // Retrieve the current text preference.  If there is no text
     50         // preference set, we need to get it from the user by invoking the
     51         // activity that retrieves it.  To do this cleanly, we will
     52         // temporarily hide our own activity so it is not displayed until the
     53         // result is returned.
     54         if (!loadPrefs()) {
     55             Intent intent = new Intent(this, RedirectGetter.class);
     56             startActivityForResult(intent, INIT_TEXT_REQUEST);
     57         }
     58     }
     59 
     60     @Override
     61 	protected void onActivityResult(int requestCode, int resultCode,
     62 		Intent data) {
     63         if (requestCode == INIT_TEXT_REQUEST) {
     64 
     65             // If the request was cancelled, then we are cancelled as well.
     66             if (resultCode == RESULT_CANCELED) {
     67                 finish();
     68 
     69             // Otherwise, there now should be text...  reload the prefs,
     70             // and show our UI.  (Optionally we could verify that the text
     71             // is now set and exit if it isn't.)
     72             } else {
     73                 loadPrefs();
     74             }
     75 
     76         } else if (requestCode == NEW_TEXT_REQUEST) {
     77 
     78             // In this case we are just changing the text, so if it was
     79             // cancelled then we can leave things as-is.
     80             if (resultCode != RESULT_CANCELED) {
     81                 loadPrefs();
     82             }
     83 
     84         }
     85     }
     86 
     87     private final boolean loadPrefs() {
     88         // Retrieve the current redirect values.
     89         // NOTE: because this preference is shared between multiple
     90         // activities, you must be careful about when you read or write
     91         // it in order to keep from stepping on yourself.
     92         SharedPreferences preferences = getSharedPreferences("RedirectData", 0);
     93 
     94         mTextPref = preferences.getString("text", null);
     95         if (mTextPref != null) {
     96             TextView text = (TextView)findViewById(R.id.text);
     97             text.setText(mTextPref);
     98             return true;
     99         }
    100 
    101         return false;
    102     }
    103 
    104     private OnClickListener mClearListener = new OnClickListener() {
    105         public void onClick(View v) {
    106             // Erase the preferences and exit!
    107             SharedPreferences preferences = getSharedPreferences("RedirectData", 0);
    108             preferences.edit().remove("text").commit();
    109             finish();
    110         }
    111     };
    112 
    113     private OnClickListener mNewListener = new OnClickListener() {
    114         public void onClick(View v) {
    115             // Retrieve new text preferences.
    116             Intent intent = new Intent(RedirectMain.this, RedirectGetter.class);
    117             startActivityForResult(intent, NEW_TEXT_REQUEST);
    118         }
    119     };
    120 
    121     private String mTextPref;
    122 }
    123