Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.raserbaden.gamlaBettan;
- import android.util.Log;
- import android.app.Activity;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.Display;
- import android.view.KeyEvent;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- //This is the activity what I have set in my AndroidManifest.xml file to be the initial activity.
- //(name not important, can be anything, no relation to 'main' convention used in C)
- public class Main extends Activity {
- //ID in log output.
- private static final String TAG = "Main";
- //Some randomly chosen ID used in message passing.
- //(in this case, the message that a new timer tick has occurred)
- static final int NEWINTERVAL = 0x1337;
- //Some useful variables.
- int activeView = 0;
- boolean isPortrait = true;
- String user = "";
- String password = "";
- //Option Menu item id:s.
- final int MAIN_ITEM_ID = 0;
- final int SETTINGS_ITEM_ID = 1;
- //A reference to a Display objects (useful later on).
- Display display;
- //A reference to view I have defined (mostly) in code.
- MainView mainView;
- TextView userTextView;
- TextView passwordTextView;
- //In my SettingsView view.
- EditText userText;
- EditText passwordText;
- //Reference to a thread used to hold timer.
- Thread tickerThread;
- //Main constructor (empty).
- public Main(){}
- //Called by system when activity created (initially or e.g. after a screen rotation).
- //(called before saved instance is restored!)
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Log.d(TAG,"Created.");
- //Remove status and title bars in landscape mode.
- Window win = getWindow();
- if(win.getWindowManager().getDefaultDisplay().getHeight() < win.getWindowManager().getDefaultDisplay().getWidth()){
- //No Statusbar.
- win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
- //No Titlebar.
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- requestWindowFeature(Window.FEATURE_PROGRESS);
- }
- //Retrieve saved data from (persistent) storage in a preferences file.
- //(file is private to this activity and has default name = activity name)
- restoreData();
- //Get screen orientation.
- display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
- if(display.getHeight() > display.getWidth()) isPortrait = true; else isPortrait = false;
- //Create my mainView.
- mainView = new MainView(this);
- //Create thread and put the ticker object in it.
- tickerThread = new Thread(new Ticker());
- tickerThread.start();//will cause the run() method to be called
- }
- //Called after the activity has been stopped, just prior to it being started again.
- //Always followed by onStart().
- @Override
- public void onRestart() {
- super.onRestart();
- Log.d(TAG,"Restarted.");
- }
- //Called just before the activity becomes visible to the user.
- //Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.
- @Override
- public void onStart() {
- super.onStart();
- Log.d(TAG,"Started.");
- }
- //Called just before the activity starts interacting with the user.
- //At this point the activity is at the top of the activity stack, with user input going to it.
- //Always followed by onPause().
- @Override
- public void onResume() {
- super.onResume();
- Log.d(TAG,"Resumed.");
- if(activeView==MAIN_ITEM_ID) {
- setContentView(mainView);
- //configMainView();
- }
- if(activeView==SETTINGS_ITEM_ID){
- setContentView(R.layout.settings);
- configSettingsView();
- }
- }
- //Called when the system is about to start resuming another activity.
- //This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on.
- //It should do whatever it does very quickly, because the next activity will not be resumed until it returns.
- //Followed either by onResume() if the activity returns back to the front, or by onStop() if it becomes invisible to the user.
- @Override
- public void onPause() {
- super.onPause();
- Log.d(TAG,"Paused.");
- }
- //Called when the activity is no longer visible to the user.
- //This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it.
- //Followed either by onRestart() if the activity is coming back to interact with the user, or by onDestroy() if this activity is going away.
- @Override
- public void onStop() {
- super.onStop();
- //Save data to (persistent) storage in a preferences file.
- //(file is private to this activity and has default name = activity name)
- saveData();
- Log.d(TAG,"Stopped.");
- }
- //Called before the activity is destroyed.
- //This is the final call that the activity will receive.
- //It could be called either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space.
- //You can distinguish between these two scenarios with the isFinishing() method.
- @Override
- public void onDestroy() {
- // Hjalmar: varför körs onDestory först?
- super.onDestroy();
- Log.d(TAG,"Destroyed.");
- //Declare ticker thread as interrupted, it will soon exit (hopefully).
- //(might need to check truly exited by calling tickerThread.isAlive() and if so, wait and kill again)
- tickerThread.interrupt();
- }
- //Called by system depending on GUI happenings.
- //(use only for GUI state, not for persistent data)
- //(if not present, system will restore GUI state itself, which might not be desirable)
- @Override
- public void onSaveInstanceState(Bundle savedInstanceState){
- super.onSaveInstanceState(savedInstanceState);
- }
- //Called by system depending on GUI happenings.
- //(use only for GUI state, not for persistent data)
- //(if not present, system will restore GUI state itself, which might not be desirable)
- @Override
- public void onRestoreInstanceState(Bundle savedInstanceState){
- super.onRestoreInstanceState(savedInstanceState);
- }
- //Save whatever data you want persistently saved (in the preferences file).
- //(file is private to this activity and has default name = activity name)
- void saveData(){
- SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
- SharedPreferences.Editor e = settings.edit();
- e.putString("User", user);
- e.putString("Password", password);
- e.commit();
- Log.d(TAG,"Saved.");
- }
- //Restore from data saved (in the preferences file).
- //(file is private to this activity and has default name = activity name)
- void restoreData(){
- SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
- user = settings.getString("User", "Elisabeth");
- password = settings.getString("Password", "password");
- Log.d(TAG,"Restored.");
- }
- //Creates Options Menu items, called first time menu accessed.
- @Override
- public boolean onCreateOptionsMenu(Menu menu){
- super.onCreateOptionsMenu(menu);
- Log.d(TAG,"Options Menu set.");
- menu.add(0, SETTINGS_ITEM_ID, 0, "Settings").setIcon(R.drawable.settings);
- return true;
- }
- //Menu handler, called when an Options Menu item is accessed.
- @Override
- public boolean onOptionsItemSelected(MenuItem item){
- activeView = item.getItemId();
- switch (item.getItemId()){
- case MAIN_ITEM_ID:
- Log.d(TAG,"Start accessed.");
- setContentView(mainView);
- configMainView();
- break;//return true;
- case SETTINGS_ITEM_ID:
- //Log.d(TAG,"Settings accessed.");
- setContentView(R.layout.settings);
- configSettingsView();
- break;//return true;
- }
- return false;
- }
- //Called right before the menu is shown, every time it is shown (not just first time menu accessed).
- //You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
- @Override
- public boolean onPrepareOptionsMenu(Menu menu) {
- return super.onPrepareOptionsMenu(menu);
- }
- void configMainView() {
- Log.d(TAG, "configMainView");
- userTextView = (TextView) mainView.findViewById(R.id.userTextView);
- userTextView.setText(user);
- passwordTextView = (TextView) mainView.findViewById(R.id.passwordTextView);
- passwordTextView.setText(password);
- }
- //Called when restoring a Settings view (no separate code for that view).
- void configSettingsView(){
- userText = (EditText) findViewById(R.id.user);
- userText.setText(user);
- passwordText = (EditText) findViewById(R.id.password);
- passwordText.setText(password);
- Button cancel = (Button) findViewById(R.id.cancel);
- cancel.setOnClickListener(new View.OnClickListener(){
- public void onClick(View v) {
- activeView = MAIN_ITEM_ID;
- setContentView(mainView);
- }
- });
- Button ok = (Button) findViewById(R.id.ok);
- ok.setOnClickListener(new View.OnClickListener(){
- public void onClick(View v) {
- user = userText.getText().toString();
- password = passwordText.getText().toString();
- saveData();
- activeView = MAIN_ITEM_ID;
- setContentView(mainView);
- }
- });
- }
- //Keyboard handler
- //Called when a key was pressed (and not handled by any of the views inside of the activity).
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event){
- if(keyCode==KeyEvent.KEYCODE_BACK){
- if(activeView!= MAIN_ITEM_ID){
- setContentView(mainView);
- activeView = MAIN_ITEM_ID;
- return true;
- }else{
- return super.onKeyDown(keyCode, event);
- }
- }
- return super.onKeyDown(keyCode, event);
- }
- //Handler for clock tick events.
- //(automatically connected to class it resides in!)
- Handler handler = new Handler(){
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- switch (msg.what) {
- case NEWINTERVAL:
- //Update time.
- /* now do lots of very serious and important work here ... :-) */
- //Log.d(TAG, "Timer tick handled.");
- //View updated.
- mainView.invalidate();
- break;//End (only) case
- }//End switch
- }//End handleMessage method
- }; //End Handler anonymous class
- //Clock ticker (runs in it's own thread).
- //(thread runs until run() method exits, in this case loops endlessly until 'interrupted' flag set to true)
- class Ticker implements Runnable {
- // Hjalmar tog bort
- //@Override
- public void run() {
- while(!Thread.currentThread().isInterrupted()){
- Message m = new Message();
- m.what = NEWINTERVAL;
- handler.sendMessage(m);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- }
- // Log.d(TAG, "Tick.");
- }
- Log.d(TAG, "Ticker thread interrupted and now exits.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement