Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ dependencies {
testCompile 'junit:junit:4.12'
compile 'com.sun.mail:android-mail:1.5.5'
compile 'com.sun.mail:android-activation:1.5.5'
compile 'com.squareup.okhttp3:okhttp:3.6.0'
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.google.android:flexbox:0.1.2'
}
9 changes: 7 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>

<application
Expand Down Expand Up @@ -62,13 +61,19 @@
android:name=".activity.AboutActivity"
android:label="关于">
</activity>
<activity android:name=".activity.StartActivity"
<activity
android:name=".activity.StartActivity"
android:theme="@style/AppTheme.Start">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".activity.RegexActivity"
android:label="正则表达式来源">
</activity>
</application>

</manifest>
156 changes: 156 additions & 0 deletions app/src/main/java/com/whf/messagerelayer/activity/RegexActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package com.whf.messagerelayer.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.flexbox.FlexboxLayout;
import com.whf.messagerelayer.R;
import com.whf.messagerelayer.utils.NativeDataManager;

import java.util.HashSet;
import java.util.Set;

/**
* Created by MTAndroidDev on 2017/11/22.
* Github: https://github.com/MTAndroidDev
* Email: ytumaotong@gmail.com
*/
public class RegexActivity extends AppCompatActivity {

private FlexboxLayout mFlexboxLayout;
private HashSet<String> mTextSet;
private NativeDataManager mNativeDataManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_regex);
initActionbar();

mNativeDataManager = new NativeDataManager(this);
mTextSet = (HashSet<String>) mNativeDataManager.getRegexSet();

this.mFlexboxLayout = (FlexboxLayout) findViewById(R.id.layout_flexbox);
initFlexboxLayout();

}
private void initActionbar(){
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("添加").setIcon(R.mipmap.ic_add)
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
showAddDialog();
return true;
}
}).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home){
finish();
}
return super.onOptionsItemSelected(item);
}

private static final String TAG = "RegexActivity";
private void showAddDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = LayoutInflater.from(this).inflate(R.layout.dialog_edit,null,false);
TextView textView = (TextView) view.findViewById(R.id.dialog_title);
final EditText editText = (EditText) view.findViewById(R.id.dialog_edit);

textView.setText("请输入关键字");
builder.setView(view);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String regex = ".*" + editText.getText().toString() + ".*";
if(regex.length()!=0){
Set<String> set = new HashSet<>(mNativeDataManager.getRegexSet());
mFlexboxLayout.addView(createItemView(regex,set.size()));
set.add(regex);
mNativeDataManager.setRegexSet(set);
Log.e(TAG, "onClick: " + regex);
}else{
Toast.makeText(RegexActivity.this,"请输入有效字符",Toast.LENGTH_LONG).show();
}
}
});

builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});
builder.show();
}

private void initFlexboxLayout() {
int i = 0;
for (String keyword:mTextSet) {
mFlexboxLayout.addView(createItemView(keyword,i++));
}
}

private View createItemView(String text, int index) {
View view = LayoutInflater.from(this).inflate(R.layout.item_keyword,null,false);
TextView textView = (TextView) view.findViewById(R.id.text_keyword);
textView.setText(text);
initBackground(textView, index);
textView.setCompoundDrawablesWithIntrinsicBounds(null, null
, getResources().getDrawable(R.mipmap.ic_del), null);

return view;
}

private void initBackground(TextView textView, int index) {
switch (index % 4) {
case 0:
textView.setBackgroundResource(R.drawable.bg_keyword_one);
break;
case 1:
textView.setBackgroundResource(R.drawable.bg_keyword_two);
break;
case 2:
textView.setBackgroundResource(R.drawable.bg_keyword_three);
break;
case 3:
textView.setBackgroundResource(R.drawable.bg_keyword_four);
break;
}
}

/**
* 点击删除其View
* @param view
*/
public void removeClick(View view){
TextView textView = (TextView) view;
String keyword = textView.getText().toString();
Set<String> set = new HashSet<>(mNativeDataManager.getRegexSet());
set.remove(keyword);
mNativeDataManager.setRegexSet(set);

mFlexboxLayout.removeView((View) view.getParent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

public class RuleActivity extends AppCompatActivity implements View.OnClickListener {

private RelativeLayout mMoblieRuleLayout, mKeywordRuleLayout, mPrefixRuleLayout, mSuffixRuleLayout;
private RelativeLayout mMoblieRuleLayout, mKeywordRuleLayout,mRegexRuleLayout, mPrefixRuleLayout, mSuffixRuleLayout;
private NativeDataManager mNativeDataManager;
private TextView mPrefixText, mSuffixText;

Expand All @@ -43,7 +43,7 @@ private void initActionbar() {
private void initView() {
mMoblieRuleLayout = (RelativeLayout) findViewById(R.id.layout_rule_mobile);
mKeywordRuleLayout = (RelativeLayout) findViewById(R.id.layout_rule_keyword);

mRegexRuleLayout = (RelativeLayout) findViewById(R.id.layout_rule_regex);
mPrefixRuleLayout = (RelativeLayout) findViewById(R.id.layout_rule_prefix);
mSuffixRuleLayout = (RelativeLayout) findViewById(R.id.layout_rule_suffix);

Expand All @@ -54,6 +54,7 @@ private void initView() {
private void initListener() {
mMoblieRuleLayout.setOnClickListener(this);
mKeywordRuleLayout.setOnClickListener(this);
mRegexRuleLayout.setOnClickListener(this);

mPrefixRuleLayout.setOnClickListener(this);
mSuffixRuleLayout.setOnClickListener(this);
Expand Down Expand Up @@ -87,6 +88,9 @@ public void onClick(View v) {
case R.id.layout_rule_keyword:
startActivity(new Intent(this, KeywordActivity.class));
break;
case R.id.layout_rule_regex:
startActivity(new Intent(this, RegexActivity.class));
break;
case R.id.layout_rule_prefix:
showEditDialog("请输入要附加的内容前缀", Constant.KEY_CONTENT_PREFIX);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public class Constant {
//关键字的列表
public static final String KEY_KEYWORD_LIST = "keyword";

// 正则表达式的列表
public static final String KEY_REGEX_LIST = "regex";


//前缀和后缀
public static final String KEY_CONTENT_PREFIX = "prefix";
public static final String KEY_CONTENT_SUFFIX = "suffix";
Expand Down
46 changes: 20 additions & 26 deletions app/src/main/java/com/whf/messagerelayer/service/SmsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

import android.app.IntentService;
import android.content.Intent;
import android.os.IBinder;
import android.provider.Telephony;
import android.util.Log;
import android.widget.Toast;

import com.whf.messagerelayer.bean.Contact;
import com.whf.messagerelayer.confing.Constant;
Expand All @@ -16,6 +12,7 @@

import java.util.ArrayList;
import java.util.Set;
import java.util.regex.Pattern;

public class SmsService extends IntentService {

Expand All @@ -34,38 +31,35 @@ public SmsService(String name) {
protected void onHandleIntent(Intent intent) {
mNativeDataManager = new NativeDataManager(this);
mDataBaseManager = new DataBaseManager(this);

String mobile = intent.getStringExtra(Constant.EXTRA_MESSAGE_MOBILE);
String content = intent.getStringExtra(Constant.EXTRA_MESSAGE_CONTENT);
Set<String> keySet = mNativeDataManager.getKeywordSet();
Set<String> regexSet = mNativeDataManager.getRegexSet();
ArrayList<Contact> contactList = mDataBaseManager.getAllContact();
//无转发规则
if (keySet.size() == 0 && contactList.size() == 0) {
relayMessage(content);
} else if (keySet.size() != 0 && contactList.size() == 0) {//仅有关键字规则

if (keySet.size() != 0) {// 关键字规则
for (String key : keySet) {
if (content.contains(key)) {
relayMessage(content);
break;
return;
}
}
} else if (keySet.size() == 0 && contactList.size() != 0) {//仅有手机号规则
}

if (contactList.size() != 0) {// 手机号规则
for (Contact contact : contactList) {
if (contact.getContactNum().equals(mobile)) {
relayMessage(content);
break;
return;
}
}
} else {//两种规则共存
out:
for (Contact contact : contactList) {
if (contact.getContactNum().equals(mobile)) {
for (String key : keySet) {
if (content.contains(key)) {
relayMessage(content);
break out;
}
}
}

if (regexSet.size() != 0) { // 正则表达式规则
for (String regex : regexSet) {
if (Pattern.matches(regex, content)) {
relayMessage(content);
return;
}
}
}
Expand All @@ -74,11 +68,11 @@ protected void onHandleIntent(Intent intent) {
private void relayMessage(String content) {
String suffix = mNativeDataManager.getContentSuffix();
String prefix = mNativeDataManager.getContentPrefix();
if(suffix!=null){
content = content+suffix;
if (suffix != null) {
content = content + suffix;
}
if(prefix!=null){
content = prefix+content;
if (prefix != null) {
content = prefix + content;
}
if (mNativeDataManager.getSmsRelay()) {
SmsRelayerManager.relaySms(mNativeDataManager, content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ public Set<String> getKeywordSet(){
return mPreference.getStringSet(Constant.KEY_KEYWORD_LIST,new HashSet<String>());
}

public void setRegexSet(Set<String> values){
mPreference.edit().putStringSet(Constant.KEY_REGEX_LIST,values).apply();
}

public Set<String> getRegexSet(){
return mPreference.getStringSet(Constant.KEY_REGEX_LIST,new HashSet<String>());
}

public void setContentPrefix(String prefix){
mPreference.edit().putString(Constant.KEY_CONTENT_PREFIX,prefix).apply();
}
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/layout/activity_regex.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_regex"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/layout_flexbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:flexWrap="wrap">
</com.google.android.flexbox.FlexboxLayout>
</RelativeLayout>
26 changes: 26 additions & 0 deletions app/src/main/res/layout/activity_rule.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@
android:src="@mipmap/ic_skip"/>
</RelativeLayout>

<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="@color/colorLine"/>
<RelativeLayout
android:id="@+id/layout_rule_regex"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="15dp"
android:background="@android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="正则表达式筛选"
android:textSize="17sp"
android:textColor="@color/colorDarkText"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:src="@mipmap/ic_skip"/>
</RelativeLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
Loading