Просмотр исходного кода

Initial commit for PersistentDataInternalStorage project.

Cristian Tanas 11 лет назад
Родитель
Сommit
9986693186

+ 27 - 0
PersistentDataInternalStorage/AndroidManifest.xml

@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="org.uab.android.persistentdata.internalstorage"
+    android:versionCode="1"
+    android:versionName="1.0" >
+
+    <uses-sdk
+        android:minSdkVersion="14"
+        android:targetSdkVersion="19" />
+
+    <application
+        android:allowBackup="true"
+        android:icon="@drawable/ic_launcher"
+        android:label="@string/app_name"
+        android:theme="@style/AppTheme" >
+        <activity
+            android:name=".MainActivity"
+            android:label="@string/app_name" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+
+</manifest>

BIN
PersistentDataInternalStorage/ic_launcher-web.png


BIN
PersistentDataInternalStorage/libs/android-support-v4.jar


BIN
PersistentDataInternalStorage/res/drawable-hdpi/ic_launcher.png


BIN
PersistentDataInternalStorage/res/drawable-mdpi/ic_launcher.png


BIN
PersistentDataInternalStorage/res/drawable-xhdpi/ic_launcher.png


BIN
PersistentDataInternalStorage/res/drawable-xxhdpi/ic_launcher.png


+ 17 - 0
PersistentDataInternalStorage/res/layout/activity_main.xml

@@ -0,0 +1,17 @@
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context="${relativePackage}.${activityClass}" >
+
+    <TextView
+        android:id="@+id/quoteTv"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignParentLeft="true"
+        android:layout_alignParentRight="true"
+        android:text="@string/hello_world"
+        android:textAppearance="?android:attr/textAppearanceMedium"
+        android:textStyle="italic" />
+
+</RelativeLayout>

+ 11 - 0
PersistentDataInternalStorage/res/values-v11/styles.xml

@@ -0,0 +1,11 @@
+<resources>
+
+    <!--
+        Base application theme for API 11+. This theme completely replaces
+        AppBaseTheme from res/values/styles.xml on API 11+ devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
+        <!-- API 11 theme customizations can go here. -->
+    </style>
+
+</resources>

+ 12 - 0
PersistentDataInternalStorage/res/values-v14/styles.xml

@@ -0,0 +1,12 @@
+<resources>
+
+    <!--
+        Base application theme for API 14+. This theme completely replaces
+        AppBaseTheme from BOTH res/values/styles.xml and
+        res/values-v11/styles.xml on API 14+ devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
+        <!-- API 14 theme customizations can go here. -->
+    </style>
+
+</resources>

+ 8 - 0
PersistentDataInternalStorage/res/values/strings.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+    <string name="app_name">PersistentDataInternalStorage</string>
+    <string name="hello_world">Hello world!</string>
+    <string name="author">Author</string>
+
+</resources>

+ 20 - 0
PersistentDataInternalStorage/res/values/styles.xml

@@ -0,0 +1,20 @@
+<resources>
+
+    <!--
+        Base application theme, dependent on API level. This theme is replaced
+        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Light">
+        <!--
+            Theme customizations available in newer API levels can go in
+            res/values-vXX/styles.xml, while customizations related to
+            backward-compatibility can go here.
+        -->
+    </style>
+
+    <!-- Application theme. -->
+    <style name="AppTheme" parent="AppBaseTheme">
+        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
+    </style>
+
+</resources>

+ 93 - 0
PersistentDataInternalStorage/src/org/uab/android/persistentdata/internalstorage/MainActivity.java

@@ -0,0 +1,93 @@
+package org.uab.android.persistentdata.internalstorage;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.util.Log;
+import android.widget.TextView;
+
+public class MainActivity extends Activity {
+	
+	private static final String		LOG_TAG = "PERSISTENT_DATA_INTERNAL_STORAGE_MAIN_ACTIVITY";
+	private static final String		FILENAME = "GRRM_Quote.txt";
+	
+	TextView						quoteTextView;
+
+	@Override
+	protected void onCreate(Bundle savedInstanceState) {
+		super.onCreate(savedInstanceState);
+		setContentView(R.layout.activity_main);
+		
+		quoteTextView = (TextView) findViewById(R.id.quoteTv);
+		
+		// Check whether the file exists on the underlying file system
+		if ( !getFileStreamPath(FILENAME).exists() ) {
+			
+			// If the text file doesn't exist, then create a new one
+			try {
+				createNewFile(FILENAME);
+			} catch (FileNotFoundException e) {
+				Log.e(LOG_TAG, "FileNotFoundException");
+			}
+		}
+		
+		// Read the data from the previously created File
+		try {
+			String fileContents = readInternalStorageFile(FILENAME);
+			quoteTextView.setText(fileContents);
+		} catch (IOException e) {
+			Log.e(LOG_TAG, "IOException");
+		}
+	}
+	
+	private void createNewFile(String filename) throws FileNotFoundException {
+		
+		FileOutputStream fos = openFileOutput(filename, MODE_PRIVATE);
+		PrintWriter writer = new PrintWriter(
+				new BufferedWriter(
+						new OutputStreamWriter(fos)
+						)
+				);
+		
+		writer.println(GRRMQuote);
+		writer.println(Author);
+		
+		writer.close();
+	}
+	
+	private String readInternalStorageFile(String filename) throws IOException {
+		
+		FileInputStream fis = openFileInput(filename);
+		BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
+		
+		StringBuilder builder = new StringBuilder();
+		String textLine = "";
+		
+		while ( (textLine = reader.readLine()) != null ) {
+			
+			builder.append(textLine);
+		}
+		
+		return builder.toString();
+	}
+	
+	static final String GRRMQuote = "\"Oh, my sweet summer child,\" Old Nan" +
+			" said quietly, \"what do you know of fear?" +
+			"Fear is for the winter, my little lord, when the snows fall a " +
+			"hundred feet deep and the ice wind comes howling out of the north. " +
+			"Fear is for the long night, when the sun hides its face for years " +
+			"at a time, and little children are born and live and die all in " +
+			"darkness while the direwolves grow gaunt and hungry, and the white " +
+			"walkers move through the woods\".";
+	
+	static final String Author = "― George R.R. Martin, A Game of Thrones";
+}

+ 27 - 0
PersistentDataSharedPreferences/src/org/uab/android/persistentdata/sharedpreferences/MainActivity.java

@@ -3,6 +3,7 @@ package org.uab.android.persistentdata.sharedpreferences;
 import java.util.Random;
 
 import android.app.Activity;
+import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
@@ -18,16 +19,23 @@ public class MainActivity extends Activity {
 	TextView		currentScoreTextView;
 	Button			playButton;
 	Button			resetButton;
+	
+	SharedPreferences	gameScoreSharedPrefs;
 
 	@Override
 	protected void onCreate(Bundle savedInstanceState) {
 		super.onCreate(savedInstanceState);
 		setContentView(R.layout.activity_main);
 		
+		// Get a reference to the SharedPreferences file
+		gameScoreSharedPrefs = getPreferences(MODE_PRIVATE);
+		
 		// Get a reference to the high score TextView
 		highScoreTextView = (TextView) findViewById(R.id.highScoreTv);
 		
 		//TODO Update the high score based on the saved high score
+		int highScore = gameScoreSharedPrefs.getInt(HIGH_SCORE_KEY, 0);
+		highScoreTextView.setText(String.valueOf(highScore));
 		
 		// Get a reference to the current game score TextView
 		currentScoreTextView = (TextView) findViewById(R.id.currentScoreTv);
@@ -47,6 +55,19 @@ public class MainActivity extends Activity {
 				
 				//TODO Check whether the current score is greater than the high score
 				//		and update the high score accordingly
+				
+				// Get store high score
+				int storedScore = gameScoreSharedPrefs.getInt(HIGH_SCORE_KEY, 0);
+				if ( value > storedScore ) {
+					
+					// Set and edit high score
+					SharedPreferences.Editor editor = gameScoreSharedPrefs.edit();
+					editor.putInt(HIGH_SCORE_KEY, value);
+					editor.commit();
+					
+					// Update the high score TextView
+					highScoreTextView.setText(String.valueOf(value));
+				}
 			}
 		});
 		
@@ -57,6 +78,12 @@ public class MainActivity extends Activity {
 			public void onClick(View v) {
 				
 				// TODO Set current game and high score to 0
+				SharedPreferences.Editor editor = gameScoreSharedPrefs.edit();
+				editor.putInt(HIGH_SCORE_KEY, 0);
+				editor.commit();
+				
+				highScoreTextView.setText(String.valueOf(0));
+				currentScoreTextView.setText(String.valueOf(0));
 			}
 		});
 	}