forked from RohitSurwase/AndroidDesignPatterns
-
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
1 parent
e395b5c
commit 7f8a3b5
Showing
26 changed files
with
672 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#This is a gitignore file | ||
#Created by "rohitss" | ||
/.gradle | ||
/.idea/ | ||
/app/build/ | ||
/app/src/androidTest | ||
/app/src/test | ||
/app/*.iml | ||
/build | ||
/*.iml | ||
/import-summary.txt | ||
/local.properties | ||
/.idea/workspace.xml | ||
/.idea/libraries | ||
.DS_Store | ||
/captures |
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 @@ | ||
/build |
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,30 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
android { | ||
compileSdkVersion 25 | ||
buildToolsVersion "25.0.2" | ||
defaultConfig { | ||
applicationId "com.rohitss.autoreadsmsotp" | ||
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.1.0' | ||
compile 'com.android.support:design:25.1.0' | ||
// testCompile 'junit:junit:4.12' | ||
} |
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,17 @@ | ||
# Add project specific ProGuard rules here. | ||
# By default, the flags in this file are appended to flags specified | ||
# in D:\Soft_Development\Android\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 *; | ||
#} |
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,27 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.rohitss.autoreadsmsotp"> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<activity android:name=".MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
<receiver | ||
android:name=".SmsReceiver" | ||
android:permission="android.permission.BROADCAST_SMS"> | ||
<intent-filter> | ||
<action android:name="android.provider.Telephony.SMS_RECEIVED" /> | ||
</intent-filter> | ||
</receiver> | ||
</application> | ||
|
||
</manifest> |
112 changes: 112 additions & 0 deletions
112
AutoReadSmsOtp/app/src/main/java/com/rohitss/autoreadsmsotp/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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.rohitss.autoreadsmsotp; | ||
|
||
import android.Manifest; | ||
import android.content.ComponentName; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.pm.PackageManager; | ||
import android.net.Uri; | ||
import android.os.Bundle; | ||
import android.provider.Settings; | ||
import android.support.annotation.NonNull; | ||
import android.support.design.widget.Snackbar; | ||
import android.support.v4.app.ActivityCompat; | ||
import android.support.v4.content.ContextCompat; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.view.View; | ||
import android.widget.Toast; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
private static final int READ_SMS_PERMISSION_CODE = 101; | ||
private Context mContext; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
mContext = MainActivity.this; | ||
//Request Permission for SMS | ||
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) { | ||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, READ_SMS_PERMISSION_CODE); | ||
} else { | ||
autoReadSmsOtp(); | ||
} | ||
} | ||
|
||
private void autoReadSmsOtp() { | ||
SmsReceiver.isAutoReadOtp = true; | ||
SmsReceiver.bindListener(new SmsListener() { | ||
@Override | ||
public void messageReceived(String messageText) { | ||
Toast.makeText(getBaseContext(), messageText, Toast.LENGTH_LONG).show(); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
protected void onResume() { | ||
super.onResume(); | ||
ComponentName receiver = new ComponentName(mContext, SmsReceiver.class); | ||
PackageManager pm = mContext.getPackageManager(); | ||
pm.setComponentEnabledSetting(receiver, | ||
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, | ||
PackageManager.DONT_KILL_APP); | ||
} | ||
|
||
@Override | ||
protected void onPause() { | ||
super.onPause(); | ||
ComponentName receiver = new ComponentName(mContext, SmsReceiver.class); | ||
PackageManager pm = mContext.getPackageManager(); | ||
pm.setComponentEnabledSetting(receiver, | ||
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, | ||
PackageManager.DONT_KILL_APP); | ||
} | ||
|
||
@Override | ||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | ||
if (grantResults.length > 0) { | ||
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { | ||
//TODO: Permissions Received, auto read otp functionality will work now | ||
autoReadSmsOtp(); | ||
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED) { | ||
// Should we show an explanation? | ||
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_SMS)) { | ||
//Show permission explanation dialog... | ||
Snackbar.make(findViewById(android.R.id.content), "Permission required to receive OTP.", Snackbar.LENGTH_LONG) | ||
.setAction("OK", new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_SMS}, READ_SMS_PERMISSION_CODE); | ||
} | ||
}) | ||
.setActionTextColor(ContextCompat.getColor(mContext, R.color.colorAccent)) | ||
.show(); | ||
} else { | ||
//Never ask again selected, or device policy prohibits the app from having that permission. | ||
//So, disable that feature, or fall back to another situation... | ||
//Open App Settings Page | ||
Snackbar.make(findViewById(android.R.id.content), "You have denied this permission. Please allow this permission.", Snackbar.LENGTH_LONG) | ||
.setAction("Settings", new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
Intent intentSettings = new Intent(); | ||
intentSettings.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); | ||
intentSettings.addCategory(Intent.CATEGORY_DEFAULT); | ||
intentSettings.setData(Uri.parse("package:" + mContext.getPackageName())); | ||
intentSettings.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
intentSettings.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); | ||
intentSettings.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); | ||
mContext.startActivity(intentSettings); | ||
} | ||
}) | ||
.setActionTextColor(ContextCompat.getColor(mContext, R.color.colorAccent)) | ||
.show(); | ||
} | ||
} | ||
} else { | ||
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | ||
} | ||
} | ||
} | ||
|
8 changes: 8 additions & 0 deletions
8
AutoReadSmsOtp/app/src/main/java/com/rohitss/autoreadsmsotp/SmsListener.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,8 @@ | ||
package com.rohitss.autoreadsmsotp; | ||
|
||
/** | ||
* Created by JayYogeshwar on 19-12-2016. | ||
*/ | ||
public interface SmsListener { | ||
void messageReceived(String strMessageBody); | ||
} |
116 changes: 116 additions & 0 deletions
116
AutoReadSmsOtp/app/src/main/java/com/rohitss/autoreadsmsotp/SmsReceiver.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,116 @@ | ||
package com.rohitss.autoreadsmsotp; | ||
|
||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.telephony.SmsMessage; | ||
|
||
/** | ||
* This class is used to Auto Read SMS Received | ||
* Created by Rohit. | ||
*/ | ||
public class SmsReceiver extends BroadcastReceiver { | ||
private static final String SMS_SENDER_NAME = "12345"; | ||
private static SmsListener mListener; | ||
public static boolean isAutoReadOtp = false; | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
Bundle myBundle = intent.getExtras(); | ||
SmsMessage[] messages = null; | ||
String strMessageSender; | ||
String strMessageBody = ""; | ||
if (myBundle != null) { | ||
Object[] pdus = (Object[]) myBundle.get("pdus"); | ||
String format = myBundle.getString("format"); | ||
if (pdus != null) { | ||
messages = new SmsMessage[pdus.length]; | ||
} | ||
if (messages != null) { | ||
for (int i = 0; i < messages.length; i++) { | ||
|
||
/** | ||
* "createFromPdu" with extra parameter "format" was introduced for Android Marshmallow to support "3gpp" | ||
* for GSM/UMTS/LTE messages in 3GPP format or "3gpp2" for CDMA/LTE messages in 3GPP2 format. | ||
*/ | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format); | ||
} else { | ||
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); | ||
} | ||
strMessageSender = messages[i].getDisplayOriginatingAddress(); | ||
//Message Sender Name | ||
if (strMessageSender.equalsIgnoreCase(SMS_SENDER_NAME)) { | ||
//Whole message text. Extract OTP from strMessageBody | ||
strMessageBody = messages[i].getMessageBody(); | ||
} | ||
} | ||
} | ||
//Pass the text to listener interface. | ||
if (isAutoReadOtp) { | ||
mListener.messageReceived(strMessageBody); | ||
} | ||
} | ||
} | ||
|
||
public static void bindListener(SmsListener listener) { | ||
mListener = listener; | ||
} | ||
} | ||
|
||
//////////////////////// | ||
/* | ||
//TODO: Steps/Helpful code | ||
//TODO: Add this class (public class SmsReceiver extends BroadcastReceiver) to your project | ||
//TODO: In Manifest Register Receiver - | ||
<application | ||
//Other Activities | ||
<receiver | ||
android:name="utils.SmsReceiver" | ||
android:permission="android.permission.BROADCAST_SMS"> | ||
<intent-filter> | ||
<action android:name="android.provider.Telephony.SMS_RECEIVED" /> | ||
</intent-filter> | ||
</receiver> | ||
</application> | ||
//TODO: Create this interface class | ||
public interface SmsListener { | ||
void messageReceived(String messageText); | ||
} | ||
//TODO: In Activity/Fragment where you want to listen/receive/autoRead SMS | ||
//TODO: 1. Enable Receiver - onCreate of Activity | ||
ComponentName receiver = new ComponentName(mContext, SmsReceiver.class); | ||
PackageManager pm = mContext.getPackageManager(); | ||
pm.setComponentEnabledSetting(receiver, | ||
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, | ||
PackageManager.DONT_KILL_APP); | ||
//TODO: 2. Disable Receiver - onPause of Activity | ||
ComponentName receiver = new ComponentName(mContext, SmsReceiver.class); | ||
PackageManager pm = mContext.getPackageManager(); | ||
pm.setComponentEnabledSetting(receiver, | ||
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, | ||
PackageManager.DONT_KILL_APP); | ||
//TODO: 3. Function to get messageText | ||
SmsReceiver.isAutoReadOtp = true; | ||
SmsReceiver.bindListener(new SmsListener() { | ||
@Override | ||
public void messageReceived(String messageText) { | ||
Toast.makeText(getBaseContext(), messageText, Toast.LENGTH_LONG).show(); | ||
} | ||
}); | ||
*/ | ||
//////////////////////// |
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,17 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/activity_main" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:paddingBottom="@dimen/activity_vertical_margin" | ||
android:paddingLeft="@dimen/activity_horizontal_margin" | ||
android:paddingRight="@dimen/activity_horizontal_margin" | ||
android:paddingTop="@dimen/activity_vertical_margin" | ||
tools:context="com.rohitss.autoreadsmsotp.MainActivity"> | ||
|
||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="Hello World!" /> | ||
</RelativeLayout> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,6 @@ | ||
<resources> | ||
<!-- Example customization of dimensions originally defined in res/values/dimens.xml | ||
(such as screen margins) for screens with more than 820dp of available width. This | ||
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). --> | ||
<dimen name="activity_horizontal_margin">64dp</dimen> | ||
</resources> |
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,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<color name="colorPrimary">#3F51B5</color> | ||
<color name="colorPrimaryDark">#303F9F</color> | ||
<color name="colorAccent">#FF4081</color> | ||
</resources> |
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,5 @@ | ||
<resources> | ||
<!-- Default screen margins, per the Android Design guidelines. --> | ||
<dimen name="activity_horizontal_margin">16dp</dimen> | ||
<dimen name="activity_vertical_margin">16dp</dimen> | ||
</resources> |
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,3 @@ | ||
<resources> | ||
<string name="app_name">Auto Read Sms Otp</string> | ||
</resources> |
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,11 @@ | ||
<resources> | ||
|
||
<!-- Base application theme. --> | ||
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> | ||
<!-- Customize your theme here. --> | ||
<item name="colorPrimary">@color/colorPrimary</item> | ||
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> | ||
<item name="colorAccent">@color/colorAccent</item> | ||
</style> | ||
|
||
</resources> |
Oops, something went wrong.