From 6a6f0ea31103cdb037a1170dcd2c5c1a4805fe45 Mon Sep 17 00:00:00 2001 From: effortlessdevsec Date: Fri, 13 Oct 2023 22:01:17 +0530 Subject: [PATCH 1/5] first_run_check --- .../ui/NavigationDrawer_Dashboard.java | 87 +++++++------------ 1 file changed, 30 insertions(+), 57 deletions(-) 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 8691ed1..9c2013f 100644 --- a/app/src/main/java/com/BugBazaar/ui/NavigationDrawer_Dashboard.java +++ b/app/src/main/java/com/BugBazaar/ui/NavigationDrawer_Dashboard.java @@ -56,7 +56,7 @@ protected void onCreate(Bundle savedInstanceState) { -// + // Intent getLink = getIntent(); // Uri data = getLink.getData(); // @@ -72,7 +72,6 @@ protected void onCreate(Bundle savedInstanceState) { // } // } - // Rest of your activity initialization code // Hide the keyboard and clear focus from the EditText View focusedView = getCurrentFocus(); @@ -113,67 +112,14 @@ protected void onCreate(Bundle savedInstanceState) { }*/ //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"); - } */ - } + // 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() { @@ -297,4 +243,31 @@ 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 From bb2d52a2ee118633eae12c5f4a760e009951b512 Mon Sep 17 00:00:00 2001 From: effortlessdevsec Date: Mon, 16 Oct 2023 11:37:46 +0530 Subject: [PATCH 2/5] timestamp --- .../BugBazaar/ui/DetailedProductActivity.java | 7 +++++- .../BugBazaar/ui/cart/CartDatabaseHelper.java | 3 +++ .../java/com/BugBazaar/ui/cart/CartItem.java | 9 +++++++- .../com/BugBazaar/ui/cart/addtimestamp.java | 22 +++++++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/BugBazaar/ui/cart/addtimestamp.java diff --git a/app/src/main/java/com/BugBazaar/ui/DetailedProductActivity.java b/app/src/main/java/com/BugBazaar/ui/DetailedProductActivity.java index 0c74252..35bf051 100644 --- a/app/src/main/java/com/BugBazaar/ui/DetailedProductActivity.java +++ b/app/src/main/java/com/BugBazaar/ui/DetailedProductActivity.java @@ -17,6 +17,7 @@ import com.BugBazaar.ui.cart.CartAdapter; import com.BugBazaar.ui.cart.CartDatabaseHelper; import com.BugBazaar.ui.cart.CartItem; +import com.BugBazaar.ui.cart.addtimestamp; import java.util.ArrayList; import java.util.List; @@ -93,8 +94,12 @@ public void onClick(View v) { long recordId = cartDBHelper.addCartItem(cartItem); // Add the new item to the database cartItems.add(cartItem); cartItem.setId(recordId); - cartItems.add(cartItem); // Add the item to the cartItems list + cartItems.add(cartItem); + Log.d("hello amit",productName); + + // Add the item to the cartItems list } + // Start CartActivity without sending the product details as Parcelable Intent intent = new Intent(DetailedProductActivity.this, CartActivity.class); startActivity(intent); diff --git a/app/src/main/java/com/BugBazaar/ui/cart/CartDatabaseHelper.java b/app/src/main/java/com/BugBazaar/ui/cart/CartDatabaseHelper.java index d8e4288..947b182 100644 --- a/app/src/main/java/com/BugBazaar/ui/cart/CartDatabaseHelper.java +++ b/app/src/main/java/com/BugBazaar/ui/cart/CartDatabaseHelper.java @@ -15,6 +15,8 @@ public class CartDatabaseHelper extends SQLiteOpenHelper { + long currentTimeMillis = System.currentTimeMillis(); + private static final String DATABASE_NAME = "cart.db"; private static final int DATABASE_VERSION = 1; @@ -67,6 +69,7 @@ public long addCartItem(CartItem cartItem) { cv.put(CartItemDBModel.CartItemEntry.COLUMN_QUANTITY, cartItem.getQuantity()); cv.put(CartItemDBModel.CartItemEntry.COLUMN_PRODIMAGE, cartItem.getImage()); + // Insert the new item into the database long recordId = sqLiteDatabase.insert(CartItemDBModel.CartItemEntry.TABLE_NAME, null, cv); sqLiteDatabase.close(); // Close the database connection diff --git a/app/src/main/java/com/BugBazaar/ui/cart/CartItem.java b/app/src/main/java/com/BugBazaar/ui/cart/CartItem.java index 7b2f0a6..5c08575 100644 --- a/app/src/main/java/com/BugBazaar/ui/cart/CartItem.java +++ b/app/src/main/java/com/BugBazaar/ui/cart/CartItem.java @@ -13,7 +13,10 @@ public class CartItem implements Parcelable { private int price; private int quantity; private long productimage; - private long id; // Unique identifier for the item in the database + private long id; + + + // Unique identifier for the item in the database // Constructor for creating a CartItem without a Product object @@ -32,6 +35,7 @@ protected CartItem(Parcel in) { price = in.readInt(); quantity = in.readInt(); productimage=in.readLong(); + } // Constructor for creating a CartItem without a Product object @@ -74,6 +78,8 @@ public void setId(long id) { this.id = id; } + + // Increment the quantity by 1 public void incrementQuantity(Context context) { if (quantity < Integer.MAX_VALUE) { // To avoid overflow @@ -124,6 +130,7 @@ public void writeToParcel(Parcel parcel, int i) { parcel.writeInt(price); parcel.writeInt(quantity); long imageResID=productimage; + parcel.writeLong(imageResID); } } diff --git a/app/src/main/java/com/BugBazaar/ui/cart/addtimestamp.java b/app/src/main/java/com/BugBazaar/ui/cart/addtimestamp.java new file mode 100644 index 0000000..1fdb025 --- /dev/null +++ b/app/src/main/java/com/BugBazaar/ui/cart/addtimestamp.java @@ -0,0 +1,22 @@ +package com.BugBazaar.ui.cart; + +import android.content.Context; +import android.content.SharedPreferences; +import android.view.View; + + +public class addtimestamp { + + private static Context context; + public static void saveCartStartTime(View.OnClickListener onClickListener, String productName) { + // Save the timestamp in SharedPreferences when an item is added to the cart + SharedPreferences sharedPreferences = context.getSharedPreferences("CartPrefs", Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + long currentTimeMillis = System.currentTimeMillis(); + editor.putString("productname", String.valueOf((productName))); +// editor.putLong("cartStartTime", 232323); + editor.apply(); + } + + +} From a9bcea306ea18ffc121fd275b4de15e3139fd681 Mon Sep 17 00:00:00 2001 From: effortlessdevsec Date: Mon, 16 Oct 2023 11:52:36 +0530 Subject: [PATCH 3/5] working_fine --- .../ui/NavigationDrawer_Dashboard.java | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) 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 202dc8a..8a8f1c8 100644 --- a/app/src/main/java/com/BugBazaar/ui/NavigationDrawer_Dashboard.java +++ b/app/src/main/java/com/BugBazaar/ui/NavigationDrawer_Dashboard.java @@ -5,6 +5,7 @@ import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; +import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.net.Uri; @@ -25,6 +26,8 @@ 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.google.android.material.navigation.NavigationView; import java.util.ArrayList; @@ -43,21 +46,14 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_navigation_drawer_dashboard); - Intent getLink = getIntent(); - Uri data = getLink.getData(); + if (AppInitializationManager.isFirstRun(this)) { + PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,new Intent(),0); - if (data != null) { - String scheme = data.getScheme(); // Get the scheme (should be "bb") - String host = data.getHost(); // Get the host (should be "bugbazaar.com") - String path = data.getPath(); // Get the path (should be "/dashboard") - - // Check if the deep link matches the expected values - if ("bb".equals(scheme) && "bugbazaar.com".equals(host) && "/dashboard".equals(path)) { - // Handle the deep link here, e.g., open the dashboard or perform other actions. - // You can also extract additional data from the deep link if needed. - } + // 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); } - // Rest of your activity initialization code // Hide the keyboard and clear focus from the EditText From 6a2e735f7a1283eb9999d50d3b66ac82187f2c1b Mon Sep 17 00:00:00 2001 From: effortlessdevsec Date: Mon, 16 Oct 2023 16:14:25 +0530 Subject: [PATCH 4/5] dynacmic code_loading --- app/build.gradle | 2 + .../java/com/BugBazaar/ui/CreatePasscode.java | 2 + .../ui/NavigationDrawer_Dashboard.java | 180 +++++++++++------- .../BugBazaar/utils/FileDownloadWorker.java | 53 ++++++ .../java/com/BugBazaar/utils/checkWorker.java | 79 ++++++++ build.gradle | 4 +- 6 files changed, 244 insertions(+), 76 deletions(-) create mode 100644 app/src/main/java/com/BugBazaar/utils/FileDownloadWorker.java create mode 100644 app/src/main/java/com/BugBazaar/utils/checkWorker.java 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..6eeb477 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,17 @@ 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.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 +45,34 @@ 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); + checkWorker check = new checkWorker(this); + + try { + check.filesendtodownload(this,getIntent().getData()); + } + catch (Exception a){ + + check.filesendtodownload(this, Uri.parse("https://github.com/banditAmit/hello/releases/download/hello/app-debug.apk")); + + + } + - // 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); } + + + //////////// first check !!!!!!! + + + + + + + ///// // Rest of your activity initialization code // Hide the keyboard and clear focus from the EditText @@ -84,78 +103,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 +238,29 @@ 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); + Intent intent = new Intent(); + intent.putExtra("price",discountedPrice); + + + + } + public void fetch_product() { @@ -286,4 +286,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/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/checkWorker.java b/app/src/main/java/com/BugBazaar/utils/checkWorker.java new file mode 100644 index 0000000..8c2b503 --- /dev/null +++ b/app/src/main/java/com/BugBazaar/utils/checkWorker.java @@ -0,0 +1,79 @@ +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) { + 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 + } + }); + } + + 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 } From ce896b8cf5bc6b4bdccea015d353d786068b51c0 Mon Sep 17 00:00:00 2001 From: effortlessdevsec Date: Mon, 16 Oct 2023 17:02:48 +0530 Subject: [PATCH 5/5] Network_Acess --- .../ui/NavigationDrawer_Dashboard.java | 24 ++++++-- .../utils/AppInitializationManager.java | 1 + .../com/BugBazaar/utils/NetworkUtils.java | 48 ++++++++++++++++ .../java/com/BugBazaar/utils/checkWorker.java | 56 +++++++++++-------- 4 files changed, 101 insertions(+), 28 deletions(-) create mode 100644 app/src/main/java/com/BugBazaar/utils/NetworkUtils.java 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 6eeb477..72e8aa0 100644 --- a/app/src/main/java/com/BugBazaar/ui/NavigationDrawer_Dashboard.java +++ b/app/src/main/java/com/BugBazaar/ui/NavigationDrawer_Dashboard.java @@ -26,6 +26,8 @@ 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; @@ -48,16 +50,28 @@ protected void onCreate(Bundle savedInstanceState) { ///// first check !!!!!! + + if (AppInitializationManager.isFirstRun(this)) { + + + checkWorker check = new checkWorker(this); try { - check.filesendtodownload(this,getIntent().getData()); - } - catch (Exception a){ + if(getIntent().getData()!=null){ + check.filesendtodownload(this,getIntent().getData()); + + } - check.filesendtodownload(this, Uri.parse("https://github.com/banditAmit/hello/releases/download/hello/app-debug.apk")); + else { + check.filesendtodownload(this, Uri.parse("https://github.com/banditAmit/hello/releases/download/hello/app-debug.apk")); + } + } + catch (Exception a){ + NetworkUtils.showExeptionDialog(this); + return; } @@ -254,8 +268,6 @@ private void handleDiscountedPrice(double discountedPrice) { AppInitializationManager.showNotification(this); CustomDialog.showCustomDialog(this, " \uD83C\uDF89 Congratulations \uD83C\uDF89", "You've received a "+ discountedPrice+"voucher.Login to Redeem",pendingIntent); AppInitializationManager.markFirstRunDone(this); - Intent intent = new Intent(); - intent.putExtra("price",discountedPrice); 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/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 index 8c2b503..0ffb7da 100644 --- a/app/src/main/java/com/BugBazaar/utils/checkWorker.java +++ b/app/src/main/java/com/BugBazaar/utils/checkWorker.java @@ -29,30 +29,42 @@ public checkWorker(Context context) { } public void filesendtodownload(NavigationDrawer_Dashboard callback, Uri data) { - 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 - } - }); + + 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;