From 0a9bec27beb0a505acc0ead47eb322d61974a8ea Mon Sep 17 00:00:00 2001 From: H Malik Date: Mon, 5 Jun 2017 21:01:55 +0100 Subject: [PATCH] Initial commit --- .gitignore | 9 + .idea/compiler.xml | 22 +++ .idea/copyright/profiles_settings.xml | 3 + .idea/gradle.xml | 18 ++ .idea/misc.xml | 46 +++++ .idea/modules.xml | 9 + .idea/runConfigurations.xml | 12 ++ app/.gitignore | 1 + app/build.gradle | 29 ++++ app/proguard-rules.pro | 17 ++ .../ExampleInstrumentedTest.java | 26 +++ app/src/main/AndroidManifest.xml | 20 +++ .../HabitContract.java | 28 +++ .../HabitDbHelper.java | 60 +++++++ .../MainActivity.java | 111 ++++++++++++ app/src/main/res/layout/activity_main.xml | 59 +++++++ app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3418 bytes app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2206 bytes app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4842 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 7718 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 10486 bytes app/src/main/res/values-w820dp/dimens.xml | 6 + app/src/main/res/values/colors.xml | 6 + app/src/main/res/values/dimens.xml | 5 + app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/styles.xml | 11 ++ .../ExampleUnitTest.java | 17 ++ build.gradle | 23 +++ gradle.properties | 17 ++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 53636 bytes gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 160 ++++++++++++++++++ gradlew.bat | 90 ++++++++++ settings.gradle | 1 + 34 files changed, 815 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/compiler.xml create mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 app/.gitignore create mode 100644 app/build.gradle create mode 100644 app/proguard-rules.pro create mode 100644 app/src/androidTest/java/com/example/h_mal/habittrackerudacityh_mal/ExampleInstrumentedTest.java create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/java/com/example/h_mal/habittrackerudacityh_mal/HabitContract.java create mode 100644 app/src/main/java/com/example/h_mal/habittrackerudacityh_mal/HabitDbHelper.java create mode 100644 app/src/main/java/com/example/h_mal/habittrackerudacityh_mal/MainActivity.java create mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 app/src/main/res/values-w820dp/dimens.xml create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/dimens.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/styles.xml create mode 100644 app/src/test/java/com/example/h_mal/habittrackerudacityh_mal/ExampleUnitTest.java create mode 100644 build.gradle create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100644 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..39fb081 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..7ac24c7 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..5d19981 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..23b732a --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..002a234 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,29 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 25 + buildToolsVersion "25.0.2" + defaultConfig { + applicationId "com.example.h_mal.habittrackerudacityh_mal" + minSdkVersion 15 + targetSdkVersion 25 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + exclude group: 'com.android.support', module: 'support-annotations' + }) + compile 'com.android.support:appcompat-v7:25.3.0' + testCompile 'junit:junit:4.12' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..05e856b --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in C:\Users\h_mal\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/app/src/androidTest/java/com/example/h_mal/habittrackerudacityh_mal/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/example/h_mal/habittrackerudacityh_mal/ExampleInstrumentedTest.java new file mode 100644 index 0000000..58d1115 --- /dev/null +++ b/app/src/androidTest/java/com/example/h_mal/habittrackerudacityh_mal/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.example.h_mal.habittrackerudacityh_mal; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumentation test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() throws Exception { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.example.h_mal.habittrackerudacityh_mal", appContext.getPackageName()); + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..4fe51bf --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/h_mal/habittrackerudacityh_mal/HabitContract.java b/app/src/main/java/com/example/h_mal/habittrackerudacityh_mal/HabitContract.java new file mode 100644 index 0000000..fbd9e53 --- /dev/null +++ b/app/src/main/java/com/example/h_mal/habittrackerudacityh_mal/HabitContract.java @@ -0,0 +1,28 @@ +package com.example.h_mal.habittrackerudacityh_mal; + +import android.provider.BaseColumns; + +/** + * Created by h_mal on 26/03/2017. + */ + +public class HabitContract { + + private HabitContract() {} + + public static final class HabitEntry implements BaseColumns { + + public final static String TABLE_NAME = "Habits"; + + + public final static String _ID = BaseColumns._ID; + + + public final static String COLUMN_HABIT_NAME ="name"; + + + public final static String COLUMN_HABIT_FREQUENCY = "frequency"; + + } + +} diff --git a/app/src/main/java/com/example/h_mal/habittrackerudacityh_mal/HabitDbHelper.java b/app/src/main/java/com/example/h_mal/habittrackerudacityh_mal/HabitDbHelper.java new file mode 100644 index 0000000..230d3aa --- /dev/null +++ b/app/src/main/java/com/example/h_mal/habittrackerudacityh_mal/HabitDbHelper.java @@ -0,0 +1,60 @@ +package com.example.h_mal.habittrackerudacityh_mal; + +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +import com.example.h_mal.habittrackerudacityh_mal.HabitContract.HabitEntry; + +/** + * Created by h_mal on 26/03/2017. + */ + +public class HabitDbHelper extends SQLiteOpenHelper { + + public static final String LOG_TAG = HabitDbHelper.class.getSimpleName(); + + private static final String DATABASE_NAME = "Habits.db"; + + private static final int DATABASE_VERSION = 1; + + public HabitDbHelper(Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + } + + @Override + public void onCreate(SQLiteDatabase db) { + + String SQL_CREATE_HABITS_TABLE = "CREATE TABLE " + HabitEntry.TABLE_NAME + " (" + + HabitEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + + HabitEntry.COLUMN_HABIT_NAME + " TEXT NOT NULL, " + + HabitEntry.COLUMN_HABIT_FREQUENCY + " INTEGER NOT NULL DEFAULT 0);"; + + db.execSQL(SQL_CREATE_HABITS_TABLE); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + } + + public Cursor readAllHabits() { + SQLiteDatabase db = getReadableDatabase(); + + String[] projection = { + HabitEntry._ID, + HabitEntry.COLUMN_HABIT_NAME, + HabitEntry.COLUMN_HABIT_FREQUENCY }; + + Cursor cursor = db.query( + HabitEntry.TABLE_NAME, + projection, + null, + null, + null, + null, + null); + return cursor; + } + +} diff --git a/app/src/main/java/com/example/h_mal/habittrackerudacityh_mal/MainActivity.java b/app/src/main/java/com/example/h_mal/habittrackerudacityh_mal/MainActivity.java new file mode 100644 index 0000000..bd06ac7 --- /dev/null +++ b/app/src/main/java/com/example/h_mal/habittrackerudacityh_mal/MainActivity.java @@ -0,0 +1,111 @@ +package com.example.h_mal.habittrackerudacityh_mal; + +import android.content.ContentValues; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.text.TextUtils; +import android.view.View; +import android.widget.EditText; +import android.widget.TextView; +import android.widget.Toast; + +import com.example.h_mal.habittrackerudacityh_mal.HabitContract.HabitEntry; + +public class MainActivity extends AppCompatActivity { + + private EditText mNameEditText; + + private EditText mFrequencyEditText; + + private HabitDbHelper mDbHelper; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + mNameEditText = (EditText) findViewById(R.id.autoCompleteTextView); + mFrequencyEditText = (EditText) findViewById(R.id.editText3); + + mDbHelper = new HabitDbHelper(this); + } + + @Override + protected void onStart() { + super.onStart(); + displayDatabaseInfo(); + } + + private void insertHabit() { + String nameString = mNameEditText.getText().toString().trim(); + String frequencyString = mFrequencyEditText.getText().toString().trim(); + + if ( + TextUtils.isEmpty(nameString) || TextUtils.isEmpty(frequencyString)) { + Toast.makeText(MainActivity.this, "please insert all data", Toast.LENGTH_SHORT).show(); + return; + } + + int frequency = Integer.parseInt(frequencyString); + + HabitDbHelper mDbHelper = new HabitDbHelper(this); + + SQLiteDatabase db = mDbHelper.getWritableDatabase(); + + ContentValues values = new ContentValues(); + + values.put(HabitEntry.COLUMN_HABIT_NAME, nameString); + + values.put(HabitEntry.COLUMN_HABIT_FREQUENCY, frequency); + + long newRowId = db.insert(HabitEntry.TABLE_NAME, null, values); + + if (newRowId == -1) { + Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(this, "New row created", Toast.LENGTH_SHORT).show(); + } + } + + + + private void displayDatabaseInfo() { + + HabitDbHelper habitDbHelper = new HabitDbHelper(this); + Cursor cursor = habitDbHelper.readAllHabits(); + + TextView displayView = (TextView) findViewById(R.id.textbox); + + try { + displayView.setText(cursor.getCount() + " Habits in habits table.\n\n"); + displayView.append(HabitEntry._ID + " - " + + HabitEntry.COLUMN_HABIT_NAME + " - " + + HabitEntry.COLUMN_HABIT_FREQUENCY + "\n"); + + int idColumnIndex = cursor.getColumnIndex(HabitEntry._ID); + int nameColumnIndex = cursor.getColumnIndex(HabitEntry.COLUMN_HABIT_NAME); + int frequencyColumnIndex = cursor.getColumnIndex(HabitEntry.COLUMN_HABIT_FREQUENCY); + + while (cursor.moveToNext()) { + int currentID = cursor.getInt(idColumnIndex); + String currentName = cursor.getString(nameColumnIndex); + int currentFrequency = cursor.getInt(frequencyColumnIndex); + displayView.append(("\n" + currentID + " - " + + currentName + " - " + + currentFrequency)); + } + } finally { + cursor.close(); + } + } + + + public void onClick(View view){ + insertHabit(); + displayDatabaseInfo(); + } + + +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..f9acbac --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + +