From 50d85b3349e1c14d258365eb6f9349c0d2b9c667 Mon Sep 17 00:00:00 2001 From: Frank Obedi Date: Tue, 26 Nov 2024 02:31:52 -0500 Subject: [PATCH 1/5] added Firebase storage and firestore --- app/build.gradle.kts | 23 ++++++++++++ app/google-services.json | 29 +++++++++++++++ .../example/camlingo/DailyQuestActivity.java | 36 ++++++++++++++++++- .../example/camlingo/DailyQuestsTasks.java | 2 +- .../com/example/camlingo/MainActivity.java | 3 ++ .../com/example/camlingo/MediaLoader.java | 29 +++++++++++++++ ...ionFragment.java => QuestionFragment.java} | 13 ++++--- .../main/res/layout/activity_daily_quest.xml | 3 +- ...age_question.xml => fragment_question.xml} | 17 +++++++-- app/src/main/res/values/strings.xml | 1 + build.gradle.kts | 3 ++ gradle/libs.versions.toml | 4 ++- 12 files changed, 149 insertions(+), 14 deletions(-) create mode 100644 app/google-services.json create mode 100644 app/src/main/java/com/example/camlingo/MediaLoader.java rename app/src/main/java/com/example/camlingo/{ImageQuestionFragment.java => QuestionFragment.java} (93%) rename app/src/main/res/layout/{fragment_image_question.xml => fragment_question.xml} (90%) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 596bade..72dbdc7 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -1,5 +1,8 @@ plugins { alias(libs.plugins.android.application) + + // Add the Google services Gradle plugin + id("com.google.gms.google-services") } android { @@ -43,6 +46,26 @@ dependencies { implementation(libs.annotation) implementation(libs.lifecycle.livedata.ktx) implementation(libs.lifecycle.viewmodel.ktx) + + // Import the Firebase BoM + implementation(platform(libs.firebase.bom)) + implementation("com.google.firebase:firebase-database") + + // Firebase Firestore + implementation("com.google.firebase:firebase-firestore") + // Firebase Storage + implementation ("com.google.firebase:firebase-storage:21.0.1") + + // TODO: Add the dependencies for Firebase products you want to use + // When using the BoM, don't specify versions in Firebase dependencies + implementation("com.google.firebase:firebase-analytics") + + // Latest Glide version + implementation("com.github.bumptech.glide:glide:4.16.0") + // Glide annotation processor + annotationProcessor("com.github.bumptech.glide:compiler:4.15.1") + + testImplementation(libs.junit) androidTestImplementation(libs.ext.junit) androidTestImplementation(libs.espresso.core) diff --git a/app/google-services.json b/app/google-services.json new file mode 100644 index 0000000..2efbd3e --- /dev/null +++ b/app/google-services.json @@ -0,0 +1,29 @@ +{ + "project_info": { + "project_number": "818416766946", + "project_id": "camlingo-app", + "storage_bucket": "camlingo-app.firebasestorage.app" + }, + "client": [ + { + "client_info": { + "mobilesdk_app_id": "1:818416766946:android:188d006520d98d125db3bb", + "android_client_info": { + "package_name": "com.example.camlingo" + } + }, + "oauth_client": [], + "api_key": [ + { + "current_key": "AIzaSyBA__3z-Y495cg0YibjIFa9G5nKwGWfugg" + } + ], + "services": { + "appinvite_service": { + "other_platform_oauth_client": [] + } + } + } + ], + "configuration_version": "1" +} \ No newline at end of file diff --git a/app/src/main/java/com/example/camlingo/DailyQuestActivity.java b/app/src/main/java/com/example/camlingo/DailyQuestActivity.java index 11b492a..8cdd6f1 100644 --- a/app/src/main/java/com/example/camlingo/DailyQuestActivity.java +++ b/app/src/main/java/com/example/camlingo/DailyQuestActivity.java @@ -1,9 +1,13 @@ package com.example.camlingo; import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.os.Bundle; +import android.util.Log; import android.view.View; import android.widget.Button; +import android.widget.ImageView; import android.widget.Toast; import androidx.activity.EdgeToEdge; @@ -14,13 +18,17 @@ import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; +import com.bumptech.glide.Glide; + +import java.io.File; +import java.io.FileInputStream; import java.util.Objects; import model.MultipleChoiceQuestion; import repository.QuestionRepository; public class DailyQuestActivity extends AppCompatActivity { - + private static final String TAG = "DailyQuestActivity"; private Button ClaimButton, StarQuestButton; @Override @@ -29,9 +37,30 @@ protected void onCreate(Bundle savedInstanceState) { EdgeToEdge.enable(this); setContentView(R.layout.activity_daily_quest); + Bitmap iconImg; + //find button by id ClaimButton = findViewById(R.id.claim_button_DailyQuest); StarQuestButton = findViewById(R.id.startQuest_button_DailyQuest); + ImageView treasureChest = findViewById(R.id.questHomeIcon); + + // check if the icon in top right exists if not download it + String fileName = "treasure-chest.png"; +// if (fileExists(fileName)){ +// try(FileInputStream fis = openFileInput(fileName)) { +// Log.i(TAG,"Found daily quest home icon locally"); +// iconImg = BitmapFactory.decodeStream(fis); +// if (iconImg == null) { +// Log.e(TAG, "Failed to decode local image file, downloading from Storage"); +// downloadIconFromStorage() +// } +// } + + Glide.with(treasureChest) + .load("https://firebasestorage.googleapis.com/v0/b/camlingo-app.firebasestorage.app/o/treasure-chest.png?alt=media&token=f2781c98-a752-49b4-836d-ca80e64fea52") + .into(treasureChest); + + //back button on toolbar Toolbar toolbar = findViewById(R.id.dailyQuest_toolbar); @@ -74,4 +103,9 @@ public void onClick(View v) { return insets; }); } + + public boolean fileExists(String fileName){ + File file = getApplicationContext().getFileStreamPath(fileName); + return file.exists(); + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/camlingo/DailyQuestsTasks.java b/app/src/main/java/com/example/camlingo/DailyQuestsTasks.java index b1cadf3..c215d71 100644 --- a/app/src/main/java/com/example/camlingo/DailyQuestsTasks.java +++ b/app/src/main/java/com/example/camlingo/DailyQuestsTasks.java @@ -43,7 +43,7 @@ protected void onCreate(Bundle savedInstanceState) { // pass question to fragment - ImageQuestionFragment fragment = ImageQuestionFragment.newInstance(questions); + QuestionFragment fragment = QuestionFragment.newInstance(questions); // display the fragment diff --git a/app/src/main/java/com/example/camlingo/MainActivity.java b/app/src/main/java/com/example/camlingo/MainActivity.java index 94652c1..0961e65 100644 --- a/app/src/main/java/com/example/camlingo/MainActivity.java +++ b/app/src/main/java/com/example/camlingo/MainActivity.java @@ -14,6 +14,9 @@ import android.widget.Toast; import java.util.Objects; +import com.google.firebase.database.DatabaseReference; +import com.google.firebase.database.FirebaseDatabase; +import com.google.firebase.firestore.FirebaseFirestore; public class MainActivity extends AppCompatActivity { diff --git a/app/src/main/java/com/example/camlingo/MediaLoader.java b/app/src/main/java/com/example/camlingo/MediaLoader.java new file mode 100644 index 0000000..cb712df --- /dev/null +++ b/app/src/main/java/com/example/camlingo/MediaLoader.java @@ -0,0 +1,29 @@ +package com.example.camlingo; +import android.util.Log; +import android.widget.ImageView; +import android.media.MediaPlayer; +import android.webkit.MimeTypeMap; + +import com.bumptech.glide.Glide; + +public class MediaLoader { + public void loadMedia(String mediaUrl, ImageView imageView){ + + // Use file extension to determine if its audio or image file + String fileExt = MimeTypeMap.getFileExtensionFromUrl(mediaUrl); + + if (fileExt != null){ + if (fileExt.equals("png")){ + // load image using Glide + Glide.with(imageView.getContext()) + .load(mediaUrl) + .into(imageView); + }else if (fileExt.equals("mp3") || fileExt.equals("wav")){ + // Load audio + Log.i("MediaLoader", "testing audio"); + } + } + } + + +} diff --git a/app/src/main/java/com/example/camlingo/ImageQuestionFragment.java b/app/src/main/java/com/example/camlingo/QuestionFragment.java similarity index 93% rename from app/src/main/java/com/example/camlingo/ImageQuestionFragment.java rename to app/src/main/java/com/example/camlingo/QuestionFragment.java index 1eb4656..0e4f8e2 100644 --- a/app/src/main/java/com/example/camlingo/ImageQuestionFragment.java +++ b/app/src/main/java/com/example/camlingo/QuestionFragment.java @@ -2,8 +2,7 @@ import android.content.Intent; import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; + import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; @@ -19,7 +18,7 @@ import java.util.List; import model.MultipleChoiceQuestion; -public class ImageQuestionFragment extends Fragment { +public class QuestionFragment extends Fragment { private Button checkBtn; private Button continueBtn; private Button finishBtn; @@ -34,13 +33,13 @@ public class ImageQuestionFragment extends Fragment { private int questionCount = 0; - public ImageQuestionFragment() { + public QuestionFragment() { // Required empty public constructor } // Fragment receives questions - public static ImageQuestionFragment newInstance(List questions) { - ImageQuestionFragment fragment = new ImageQuestionFragment(); + public static QuestionFragment newInstance(List questions) { + QuestionFragment fragment = new QuestionFragment(); Bundle args = new Bundle(); args.putSerializable("questions", new ArrayList<>(questions)); fragment.setArguments(args); @@ -51,7 +50,7 @@ public static ImageQuestionFragment newInstance(List que public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment - View view = inflater.inflate(R.layout.fragment_image_question, container, false); + View view = inflater.inflate(R.layout.fragment_question, container, false); // Initialize views checkBtn = view.findViewById(R.id.checkBtn); diff --git a/app/src/main/res/layout/activity_daily_quest.xml b/app/src/main/res/layout/activity_daily_quest.xml index 7b52b17..44e34dc 100644 --- a/app/src/main/res/layout/activity_daily_quest.xml +++ b/app/src/main/res/layout/activity_daily_quest.xml @@ -78,11 +78,12 @@ + /> diff --git a/app/src/main/res/layout/fragment_image_question.xml b/app/src/main/res/layout/fragment_question.xml similarity index 90% rename from app/src/main/res/layout/fragment_image_question.xml rename to app/src/main/res/layout/fragment_question.xml index fe1a3ad..147b658 100644 --- a/app/src/main/res/layout/fragment_image_question.xml +++ b/app/src/main/res/layout/fragment_question.xml @@ -4,7 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" - tools:context=".ImageQuestionFragment"> + tools:context=".QuestionFragment"> +