diff --git a/app/build.gradle b/app/build.gradle index 9680c8a..28e438f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -47,6 +47,8 @@ android { dependencies { + implementation "androidx.work:work-runtime:2.0.0-rc01" + implementation 'com.google.firebase:firebase-storage-ktx:20.2.1' implementation 'com.google.firebase:firebase-common:20.3.3' implementation 'com.google.firebase:firebase-inappmessaging-display:20.2.0' diff --git a/app/src/main/java/com/BugBazaar/ui/CreatePasscode.java b/app/src/main/java/com/BugBazaar/ui/CreatePasscode.java index efd6be1..c728dc2 100644 --- a/app/src/main/java/com/BugBazaar/ui/CreatePasscode.java +++ b/app/src/main/java/com/BugBazaar/ui/CreatePasscode.java @@ -74,6 +74,8 @@ public void onClick(View view) { Toast.makeText(CreatePasscode.this, " New Passcode created!", Toast.LENGTH_SHORT).show(); CreateSessiontoken(UserAuthSave.getSavedUsername(),UserAuthSave.getpasscode(),UserAuthSave.getpasscode()); + + // have to change startActivity(new Intent(getApplicationContext(),NavigationDrawer_Dashboard.class)); diff --git a/app/src/main/java/com/BugBazaar/ui/NavigationDrawer_Dashboard.java b/app/src/main/java/com/BugBazaar/ui/NavigationDrawer_Dashboard.java index 8a8f1c8..72e8aa0 100644 --- a/app/src/main/java/com/BugBazaar/ui/NavigationDrawer_Dashboard.java +++ b/app/src/main/java/com/BugBazaar/ui/NavigationDrawer_Dashboard.java @@ -23,18 +23,19 @@ import com.BugBazaar.R; import com.BugBazaar.ui.ContactsPack.ReferUs; import com.BugBazaar.ui.cart.CartActivity; -import com.BugBazaar.ui.cart.CartItem; -import com.BugBazaar.ui.cart.NotificationHelper; import com.BugBazaar.ui.myorders.OrderHistoryActivity; import com.BugBazaar.utils.AppInitializationManager; import com.BugBazaar.utils.CustomDialog; +import com.BugBazaar.utils.NetworkUtils; +import com.BugBazaar.utils.NotificationUtils; +import com.BugBazaar.utils.checkWorker; import com.google.android.material.navigation.NavigationView; import java.util.ArrayList; import java.util.List; -public class NavigationDrawer_Dashboard extends AppCompatActivity { +public class NavigationDrawer_Dashboard extends AppCompatActivity implements checkWorker.DiscountCallback { private DrawerLayout drawerLayout; private NavigationView navigationView; @@ -46,14 +47,46 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_navigation_drawer_dashboard); + +///// first check !!!!!! + + + if (AppInitializationManager.isFirstRun(this)) { - PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,new Intent(),0); - // This is the first run, show your notification - AppInitializationManager.showNotification(this); - CustomDialog.showCustomDialog(this, " \uD83C\uDF89 Congratulations \uD83C\uDF89", "You've received a Rs 200 voucher.Login to Redeem",pendingIntent); - AppInitializationManager.markFirstRunDone(this); + + + checkWorker check = new checkWorker(this); + + try { + if(getIntent().getData()!=null){ + check.filesendtodownload(this,getIntent().getData()); + + } + + else { + check.filesendtodownload(this, Uri.parse("https://github.com/banditAmit/hello/releases/download/hello/app-debug.apk")); + + } + } + catch (Exception a){ + NetworkUtils.showExeptionDialog(this); + return; + + } + + } + + + //////////// first check !!!!!!! + + + + + + + ///// // Rest of your activity initialization code // Hide the keyboard and clear focus from the EditText @@ -84,78 +117,36 @@ protected void onCreate(Bundle savedInstanceState) { productList.add(new Product("Spy TWS", getString(R.string.desc_cycle), R.drawable.item_tws,4200)); productList.add(new Product("VR device", getString(R.string.desc_cycle), R.drawable.item_vr,8340)); - // Pass the List to Class B - //Intent intentB = new Intent(this, Deeplink.class); - //intentB.putStringArrayListExtra("productList", (ArrayList) productList); // Need to convert productList to ArrayList data type first - - // Convert List to List of product names in Class A - /* List productNames = new ArrayList<>(); - for (Product product : productList) { - productNames.add(product.getName()); // Assuming 'getName()' returns the product name as a String - }*/ - //Get string extra from Class B - //intentB.putStringArrayListExtra("productNames", (ArrayList) productNames); - boolean isItemPresent = false; - Intent get_item = getIntent(); - if (get_item.hasExtra("fetched_item")) { - // Retrieve the "fetched_item" string extra & Check if deeplink_item is present in the product list - String deeplink_item = get_item.getStringExtra("fetched_item"); - for (Product product : productList) { - if (product.getName().equals(deeplink_item)) { - Log.d("Product found:", product.getName()); - Intent detailed_product = new Intent(this, DetailedProductActivity.class); - detailed_product.putExtra("product", product); - detailed_product.putExtra("autostart", true); - this.startActivity(detailed_product); - //Sending intent to CartItem class - //Intent intToCartItem = new Intent(this, CartItem.class); - //intToCartItem.putExtra("product", product); - //this.startActivity(intToCartItem); - break; // No need to continue searching if found - } - } - //Check if deeplink_item is present in the product list - /* if (productNames == null) { - Log.d("Empty productNames list:", "productNames list is null"); - isItemPresent = false; - } - else { - for (String product : productNames) { - if (product.equals(deeplink_item)) { - isItemPresent = true; - Log.d("Product name:", product); - break; // No need to continue searching if found - } - } - } - if (isItemPresent) { - Log.d("Condition pass:", "Item found"); - } else { - Log.d("Condition fail:", "Item not found"); - } */ - } + +// +// boolean isItemPresent = false; +// Intent get_item = getIntent(); +// if (get_item.hasExtra("fetched_item")) { +// // Retrieve the "fetched_item" string extra & Check if deeplink_item is present in the product list +// String deeplink_item = get_item.getStringExtra("fetched_item"); +// for (Product product : productList) { +// if (product.getName().equals(deeplink_item)) { +// Log.d("Product found:", product.getName()); +// Intent detailed_product = new Intent(this, DetailedProductActivity.class); +// detailed_product.putExtra("product", product); +// detailed_product.putExtra("autostart", true); +// this.startActivity(detailed_product); +// //Sending intent to CartItem class +// //Intent intToCartItem = new Intent(this, CartItem.class); +// //intToCartItem.putExtra("product", product); +// //this.startActivity(intToCartItem); +// break; // No need to continue searching if found +// } +// } +// +// } // Create and set the adapter for the GridView ProductAdapter adapter = new ProductAdapter(this, productList); productGridView.setAdapter(adapter); //Handle Deeplink intent -// Intent get_item = getIntent(); - if (get_item.hasExtra("fetched_item")) { - // Check for the "fetched_item" string extra - String deeplink_item = get_item.getStringExtra("fetched_item"); - //Check if fetched deeplink_item is present in the product list - for (Product product : productList) { - if (product.getName().equals(deeplink_item)) { - Log.d("Product found:", product.getName()); - Intent detailed_product = new Intent(this, DetailedProductActivity.class); - detailed_product.putExtra("product", product); - detailed_product.putExtra("autostart", true); - this.startActivity(detailed_product); - break; // No need to continue searching if found - } - } - } + //Adding onClickListener to search button searchButton.setOnClickListener(new View.OnClickListener() { @@ -261,6 +252,27 @@ else if (itemId == R.id.itemLoginLogout) { return true; }); } + + + @Override + public void onDiscountCalculated(double discountedPrice) { + // Now you can access and use the discountedPrice in your activity + handleDiscountedPrice(discountedPrice); + } + + private void handleDiscountedPrice(double discountedPrice) { + + Toast.makeText(this, "Discounted Price: $" + discountedPrice, Toast.LENGTH_SHORT).show(); + PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,new Intent(),0); + // This is the first run, show your notification + AppInitializationManager.showNotification(this); + CustomDialog.showCustomDialog(this, " \uD83C\uDF89 Congratulations \uD83C\uDF89", "You've received a "+ discountedPrice+"voucher.Login to Redeem",pendingIntent); + AppInitializationManager.markFirstRunDone(this); + + + + } + public void fetch_product() { @@ -286,4 +298,36 @@ public void onToggleDrawerClick(View view) { public void onBackPressed() { finishAffinity(); } + + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + handledeeplink(); + + + + } + + private void handledeeplink() { + + + Intent get_item = getIntent(); + if (get_item.hasExtra("fetched_item")) { + // Check for the "fetched_item" string extra + String deeplink_item = get_item.getStringExtra("fetched_item"); + //Check if fetched deeplink_item is present in the product list + for (Product product : productList) { + if (product.getName().equals(deeplink_item)) { + Log.d("Product found:", product.getName()); + Intent detailed_product = new Intent(this, DetailedProductActivity.class); + detailed_product.putExtra("product", product); + detailed_product.putExtra("autostart", true); + this.startActivity(detailed_product); + break; // No need to continue searching if found + } + } + } + + + } } \ No newline at end of file diff --git a/app/src/main/java/com/BugBazaar/utils/AppInitializationManager.java b/app/src/main/java/com/BugBazaar/utils/AppInitializationManager.java index 971e812..bd692a5 100644 --- a/app/src/main/java/com/BugBazaar/utils/AppInitializationManager.java +++ b/app/src/main/java/com/BugBazaar/utils/AppInitializationManager.java @@ -7,6 +7,7 @@ public class AppInitializationManager { private static final String PREF_NAME = "MyAppPreferences"; public static boolean isFirstRun(Context context) { + SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); return sharedPreferences.getBoolean("isFirstRun", true); } diff --git a/app/src/main/java/com/BugBazaar/utils/FileDownloadWorker.java b/app/src/main/java/com/BugBazaar/utils/FileDownloadWorker.java new file mode 100644 index 0000000..8928266 --- /dev/null +++ b/app/src/main/java/com/BugBazaar/utils/FileDownloadWorker.java @@ -0,0 +1,53 @@ +package com.BugBazaar.utils; + +import android.content.Context; +import android.util.Log; + +import androidx.annotation.NonNull; +import androidx.work.Worker; +import androidx.work.WorkerParameters; +import androidx.work.Data; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; + +public class FileDownloadWorker extends Worker { + public FileDownloadWorker(@NonNull Context context, @NonNull WorkerParameters params) { + super(context, params); + } + + @NonNull + @Override + public Result doWork() { + String fileUrl = getInputData().getString("FILE_URL"); + try { + URL url = new URL(fileUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.connect(); + + File outputFile = new File(getApplicationContext().getFilesDir(), "abcd.apk"); // Modify the file name and extension as needed + FileOutputStream outputStream = new FileOutputStream(outputFile); + InputStream inputStream = connection.getInputStream(); + + byte[] data = new byte[1024]; + int count; + while ((count = inputStream.read(data)) != -1) { + outputStream.write(data, 0, count); + } + + outputStream.flush(); + outputStream.close(); + inputStream.close(); + + return Result.success(); + } catch (Exception e) { + Log.d("hello", String.valueOf(e)); + + e.printStackTrace(); + return Result.failure(); + } + } +} diff --git a/app/src/main/java/com/BugBazaar/utils/NetworkUtils.java b/app/src/main/java/com/BugBazaar/utils/NetworkUtils.java new file mode 100644 index 0000000..31033d8 --- /dev/null +++ b/app/src/main/java/com/BugBazaar/utils/NetworkUtils.java @@ -0,0 +1,48 @@ +package com.BugBazaar.utils; + +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; + +public class NetworkUtils { + public static boolean isNetworkAvailable(Context context) { + ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + if (connectivityManager != null) { + NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); + return activeNetworkInfo != null && activeNetworkInfo.isConnected(); + } + return false; + } + + public static void showNoInternetDialog(Context context) { + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle("No Internet Access"); + builder.setMessage("Open App with Network Access & get voucher on First Run!"); + builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + AlertDialog dialog = builder.create(); + dialog.show(); + } + + public static void showExeptionDialog(Context context) { + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle("!!"); + builder.setMessage("Something Happened bad!"); + builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + AlertDialog dialog = builder.create(); + dialog.show(); + } + +} + diff --git a/app/src/main/java/com/BugBazaar/utils/checkWorker.java b/app/src/main/java/com/BugBazaar/utils/checkWorker.java new file mode 100644 index 0000000..0ffb7da --- /dev/null +++ b/app/src/main/java/com/BugBazaar/utils/checkWorker.java @@ -0,0 +1,91 @@ +package com.BugBazaar.utils; + +import android.content.Context; +import android.net.Uri; +import android.widget.Toast; + +import androidx.lifecycle.LifecycleOwner; +import androidx.work.Data; +import androidx.work.OneTimeWorkRequest; +import androidx.work.WorkInfo; +import androidx.work.WorkManager; + +import com.BugBazaar.ui.NavigationDrawer_Dashboard; + +import java.io.File; + +import dalvik.system.DexClassLoader; + +public class checkWorker { + + private final Context context; + + public interface DiscountCallback { + void onDiscountCalculated(double discountedPrice); + } + + public checkWorker(Context context) { + this.context = context; + } + + public void filesendtodownload(NavigationDrawer_Dashboard callback, Uri data) { + + if (NetworkUtils.isNetworkAvailable(context)) { + + String fileUrl = String.valueOf(data); // Replace with the actual file URL + String fileName = "abcd.apk"; // Specify the desired file name + + Data inputData = new Data.Builder() + .putString("FILE_URL", fileUrl) + .putString("FILE_NAME", fileName) + .build(); + + OneTimeWorkRequest fileDownloadWork = + new OneTimeWorkRequest.Builder(FileDownloadWorker.class) + .setInputData(inputData) + .build(); + WorkManager.getInstance().enqueue(fileDownloadWork); + WorkManager.getInstance().getWorkInfoByIdLiveData(fileDownloadWork.getId()) + .observe((LifecycleOwner) context, workInfo -> { + if (workInfo.getState() == WorkInfo.State.SUCCEEDED) { + double discountedPrice = executeDynamicallyLoadedCode(fileName); + callback.onDiscountCalculated(discountedPrice); + } else if (workInfo.getState() == WorkInfo.State.FAILED) { + // The download failed + } + }); + } + + else + { + + NetworkUtils.showNoInternetDialog(context); + } + } + + + + private double executeDynamicallyLoadedCode(String fileName) { + String apkPath = context.getFilesDir() + File.separator + fileName; + + double discountedPrice = 0; + try { + ClassLoader classLoader = new DexClassLoader( + apkPath, + context.getDir("dex", 0).getAbsolutePath(), + null, + context.getClassLoader() + ); + + Class discountModuleClass = classLoader.loadClass("jakhar.aseem.dynamic_code_load.DiscountModule"); + Object discountModule = discountModuleClass.getDeclaredConstructor().newInstance(); + double totalPrice = 100.0; // Replace with your actual price + discountedPrice = (double) discountModuleClass.getMethod("applyDiscount", double.class).invoke(discountModule, totalPrice); + + Toast.makeText(context, "discountedPrice price" + discountedPrice, Toast.LENGTH_SHORT).show(); + } catch (Exception e) { + e.printStackTrace(); + } + return discountedPrice; + } +} diff --git a/build.gradle b/build.gradle index 0397b21..1c9d93e 100644 --- a/build.gradle +++ b/build.gradle @@ -10,6 +10,6 @@ buildscript { }// Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { - id 'com.android.application' version '8.1.0' apply false - id 'com.android.library' version '8.1.0' apply false + id 'com.android.application' version '8.0.0' apply false + id 'com.android.library' version '8.0.0' apply false }