-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
elegidocodes
committed
Jan 9, 2025
1 parent
94556c7
commit f0edfad
Showing
6 changed files
with
117 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
app/src/main/java/com/elegidocodes/android/util/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,16 @@ | ||
package com.elegidocodes.android.util; | ||
|
||
import android.os.Bundle; | ||
import android.widget.TextView; | ||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
|
||
import com.elegidocodes.android.util.theme.ThemeUtil; | ||
|
||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
|
||
TextView textView = findViewById(R.id.textView); | ||
|
||
if (ThemeUtil.isDarkModeEnabled(this)) { | ||
textView.setText("Dark Mode is Enabled"); | ||
} else { | ||
textView.setText("Dark Mode is Disabled"); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
util/src/main/java/com/elegidocodes/android/util/network/UrlUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.elegidocodes.android.util.network; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.net.Uri; | ||
|
||
public class UrlUtil { | ||
|
||
/** | ||
* Determines if the given string is a valid URL starting with "http://" or "https://". | ||
* | ||
* <p>This method checks if the input string is non-null and starts with a valid HTTP or HTTPS prefix.</p> | ||
* | ||
* <p>Example usage: | ||
* <pre>{@code | ||
* String data = "https://www.example.com"; | ||
* boolean isValidUrl = UrlUtils.isUrl(data); // Returns true | ||
* }</pre> | ||
* </p> | ||
* | ||
* @param data The string to be checked. | ||
* @return {@code true} if the string is a valid URL, {@code false} otherwise. | ||
*/ | ||
public static boolean isUrl(String data) { | ||
return data != null && (data.startsWith("http://") || data.startsWith("https://")); | ||
} | ||
|
||
/** | ||
* Opens the specified URL in the default browser or associated app on the user's device. | ||
* | ||
* <p>This method constructs an intent with the given URL and starts an activity to handle the URL. | ||
* Ensure that the URL is valid before calling this method.</p> | ||
* | ||
* <p>Example usage: | ||
* <pre>{@code | ||
* String url = "https://www.example.com"; | ||
* UrlUtils.openUrl(context, url); // Opens the URL | ||
* }</pre> | ||
* </p> | ||
* | ||
* @param context The context used to start the activity. | ||
* @param url The URL to be opened. | ||
* @throws IllegalArgumentException if the provided URL is null or empty. | ||
*/ | ||
public static void openUrl(Context context, String url) { | ||
if (url == null || url.isEmpty()) { | ||
throw new IllegalArgumentException("URL cannot be null or empty"); | ||
} | ||
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); | ||
context.startActivity(intent); | ||
|
||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
util/src/main/java/com/elegidocodes/android/util/zxing/QRCodeUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.elegidocodes.android.util.zxing; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.Color; | ||
|
||
import com.google.zxing.BarcodeFormat; | ||
import com.google.zxing.MultiFormatWriter; | ||
import com.google.zxing.WriterException; | ||
import com.google.zxing.common.BitMatrix; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
|
||
public class QRCodeUtil { | ||
|
||
/** | ||
* Generates a QR Code from the provided URL as a Bitmap. | ||
* | ||
* <p>This method uses the {@link MultiFormatWriter} to encode the given URL into a QR code | ||
* and creates a Bitmap representation of it.</p> | ||
* | ||
* @param url The URL to encode in the QR code. | ||
* @return A {@link Bitmap} containing the QR code. | ||
* @throws WriterException If the QR code encoding fails. | ||
*/ | ||
public static Bitmap generateQRCodeBitmap(String url) throws WriterException { | ||
MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); | ||
BitMatrix bitMatrix = multiFormatWriter.encode(url, BarcodeFormat.QR_CODE, 200, 200); | ||
int width = bitMatrix.getWidth(); | ||
int height = bitMatrix.getHeight(); | ||
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); | ||
|
||
for (int x = 0; x < width; x++) { | ||
for (int y = 0; y < height; y++) { | ||
bitmap.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE); | ||
} | ||
} | ||
|
||
return bitmap; | ||
} | ||
|
||
/** | ||
* Generates a QR Code from the provided URL as a byte array in PNG format. | ||
* | ||
* <p>This method uses the {@link MultiFormatWriter} to encode the given URL into a QR code, | ||
* converts it to a Bitmap, and then compresses it into a PNG byte array.</p> | ||
* | ||
* @param url The URL to encode in the QR code. | ||
* @return A byte array containing the QR code image in PNG format. | ||
* @throws WriterException If the QR code encoding fails. | ||
*/ | ||
public static byte[] generateQRCodeBytes(String url) throws WriterException { | ||
Bitmap bitmap = generateQRCodeBitmap(url); | ||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | ||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); | ||
return byteArrayOutputStream.toByteArray(); | ||
} | ||
|
||
} |