Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into dong
Browse files Browse the repository at this point in the history
  • Loading branch information
wenzhe-li committed Sep 6, 2019
2 parents ec5f785 + b0c75c7 commit b1a8d4f
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 6 deletions.
9 changes: 9 additions & 0 deletions app/src/main/java/com/java/lwzdhw/bean/News.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,14 @@ public String toString() {
public int hashCode() {
return newsID.hashCode();
}

@Override
public boolean equals(Object obj) {
if(obj.getClass() == News.class){
News temp = (News) obj;
return newsID.equals(temp.newsID);
}
return super.equals(obj);
}
}

31 changes: 29 additions & 2 deletions app/src/main/java/com/java/lwzdhw/fragment/NewsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.java.lwzdhw.bean.News;
import com.java.lwzdhw.bean.Page;
import com.java.lwzdhw.utils.GetDate;
import com.java.lwzdhw.utils.MaskHandler;
import com.java.lwzdhw.utils.Sample;
import com.java.lwzdhw.utils.ServerHandler;
import com.java.lwzdhw.view.HeadListView;
Expand All @@ -26,6 +27,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class NewsFragment extends Fragment {
private final static String TAG = "NewsFragment";
Expand Down Expand Up @@ -121,6 +123,15 @@ public void run() {
news = page.data;
Log.d(TAG, "run: " + categoryId + " " + text + " " + news.length + " " + moreTimes);
newsList = new ArrayList<>(Arrays.asList(news));
Collections.shuffle(newsList);
int masked = 0;
for (int i = 0 ; i < newsList.size() ; ++i){
if(masked > 15) break;
if(!MaskHandler.getInstance().check(newsList.get(i))){
newsList.remove(i);
masked ++;
}
}
newsList = (ArrayList<News>) Sample.createRandomList(newsList, 10);
handler.obtainMessage(SET_NEWSLIST).sendToTarget();
refreshLayout.finishRefresh();
Expand All @@ -134,14 +145,21 @@ public void getMoreData() {
@Override
public void run() {
serverHandler = new ServerHandler();
serverHandler.setSize(newsListSize + 10 * moreTimes);
serverHandler.setSize(newsList.size() + 10);
serverHandler.setCategories(text);
serverHandler.setEndDate(GetDate.getCurrentDate());
try {
page = serverHandler.getPage();
news = page.data;
Log.d(TAG, "run: " + categoryId + " " + text + " " + news.length + " " + moreTimes);
for (int i = newsList.size(); i < news.length; i++) {
int masked = 0;
for (int i = 0; i < news.length; i++) {
// Log.d("TryHas",news[i].title);
if(newsList.contains(news[i])) continue;
if(masked < 5 && !MaskHandler.getInstance().check(news[i])){
masked ++;
continue;
}
newsList.add(news[i]);
}
handler.obtainMessage(MORE_NEWS).sendToTarget();
Expand Down Expand Up @@ -176,6 +194,15 @@ public void run() {
news = page.data;
Log.d(TAG, "run: " + categoryId + " " + text + " " + news.length + " " + moreTimes);
newsList = new ArrayList<>(Arrays.asList(news));
Collections.shuffle(newsList);
int masked = 0;
for (int i = 0 ; i < newsList.size() ; ++i){
if(masked > 15) break;
if(!MaskHandler.getInstance().check(newsList.get(i))){
newsList.remove(i);
masked ++;
}
}
newsList = (ArrayList<News>) Sample.createRandomList(newsList, 10);
handler.obtainMessage(SET_NEWSLIST).sendToTarget();
}
Expand Down
18 changes: 17 additions & 1 deletion app/src/main/java/com/java/lwzdhw/fragment/ResultFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.java.lwzdhw.bean.News;
import com.java.lwzdhw.bean.Page;
import com.java.lwzdhw.utils.GetDate;
import com.java.lwzdhw.utils.MaskHandler;
import com.java.lwzdhw.utils.ServerHandler;
import com.java.lwzdhw.view.HeadListView;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
Expand Down Expand Up @@ -101,7 +102,13 @@ public void run() {
try {
page = serverHandler.getPage();
news = page.data;
for (int i = newsList.size(); i < news.length; i++) {
int masked = 0;
for (int i = 0 ; i < news.length; i++) {
if(newsList.contains(news[i])) continue;
if(masked < 5 && !MaskHandler.getInstance().check(news[i])){
masked ++;
continue;
}
newsList.add(news[i]);
}
handler.obtainMessage(MORE_NEWS).sendToTarget();
Expand All @@ -125,6 +132,7 @@ public void run() {
serverHandler.setSize(10);
if (keywords != null) serverHandler.setWords(keywords);
serverHandler.setEndDate(GetDate.getCurrentDate());

while (page == null) {
page = serverHandler.getPage();
try {
Expand All @@ -135,6 +143,14 @@ public void run() {
}
news = page.data;
newsList = new ArrayList<>(Arrays.asList(news));
int masked = 0;
for (int i = 0 ; i < newsList.size() ; ++i){
if(masked > 5) break;
if(!MaskHandler.getInstance().check(newsList.get(i))){
newsList.remove(i);
masked ++;
}
}
handler.obtainMessage(SET_NEWSLIST).sendToTarget();
}
}).start();
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/java/com/java/lwzdhw/utils/MaskHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.java.lwzdhw.utils;

import com.java.lwzdhw.bean.News;

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

public class MaskHandler {
private static MaskHandler instance = null;
private Set<String> badWords = new HashSet<>();
final private static double minThreshold = 0.5;

private MaskHandler(){

}
public void add(final String word){
badWords.add(word);
}

public void remove(final String word){
badWords.remove(word);
}

public void flush(){
badWords.clear();
}

public boolean check(final News news){
if(badWords.size() == 0) return true;
News.ScoreWord[] keywords = news.getKeywords();
Vector<String> vector = new Vector<>();
for (int i = 0 ; i < keywords.length ; ++i){
if (keywords[i].score > minThreshold && badWords.contains(keywords[i].word))
return false;
}
return true;
}

public static MaskHandler getInstance(){
if(instance == null) {
instance = new MaskHandler();
}
return instance;
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/java/lwzdhw/utils/RecordHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void remove(final String newsID){
}
public void save(){
try {
Log.d(TAG,"Save"+recordFile.getAbsolutePath());
// Log.d(TAG,"Save"+recordFile.getAbsolutePath());
recordFile.delete();
recordFile.createNewFile();
BufferedWriter writer = new BufferedWriter(new FileWriter(recordFile));
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/java/lwzdhw/utils/Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class Sample {
public static List createRandomList(List list, int n) {
// TODO Auto-generated method stub
if(list.size() < n) n = list.size();
Map map = new HashMap();
List listNew = new ArrayList();
if (list.size() <= n) {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/menu/activity_main_drawer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
android:id="@+id/nav_login"
android:icon="@mipmap/ic_account_box_black_48dp"
android:title="@string/menu_login" />
<item
android:id="@+id/nav_mask"
android:icon="@mipmap/ic_account_box_black_48dp"
android:title="@string/menu_mask" />
</group>

<item android:title="主题">
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
<string name="menu_favorite">收藏</string>
<string name="menu_history">历史</string>
<string name="menu_login">登录</string>
<string name="menu_share">Share</string>
<string name="menu_send">Send</string>
<string name="menu_mask">屏蔽</string>

<string name="title_activity_login">Sign in</string>
<string name="prompt_email">Email</string>
Expand Down

0 comments on commit b1a8d4f

Please sign in to comment.