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 5, 2019
2 parents 9b1d1d1 + 744e843 commit 6ab8c23
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 58 deletions.
5 changes: 0 additions & 5 deletions app/src/main/java/com/example/topnews/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
Expand Down
103 changes: 61 additions & 42 deletions app/src/main/java/com/example/topnews/data/LoginDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

/**
* Class that handles authentication w/ login credentials and retrieves user information.
Expand All @@ -36,18 +37,18 @@ public Result<LoggedInUser> signup(String username, String password) {
bundle.setItem(WebPackage.DATA_ID,username);
bundle.setItem(WebPackage.DATA_PASS,password);
userClient.setWebPackage(bundle);
userClient.run();
userClient.start();

Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
lost = true;
}
}, 3000);
}, waitTime);

while (!lost && userClient.getResponse() == null){
wait();

}

WebPackage response = userClient.getResponse();
Expand Down Expand Up @@ -76,18 +77,18 @@ public Result<LoggedInUser> login(String username, String password) {
bundle.setItem(WebPackage.DATA_ID,username);
bundle.setItem(WebPackage.DATA_PASS,password);
userClient.setWebPackage(bundle);
userClient.run();
userClient.start();

Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
lost = true;
}
}, 3000);
}, waitTime);

while (!lost && userClient.getResponse() == null){
wait();

}

WebPackage response = userClient.getResponse();
Expand All @@ -113,18 +114,18 @@ private long getModify(){
bundle.setItem(WebPackage.DATA_OP,WebPackage.OP_GET_MODIFY);
bundle.setItem(WebPackage.DATA_ID,MainActivity.user.getUserId());
userClient.setWebPackage(bundle);
userClient.run();
userClient.start();

Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
lost = true;
}
}, 3000);
}, waitTime);
try {
while (!lost && userClient.getResponse() == null) {
wait();

}
return Long.valueOf(userClient.getResponse().getString(WebPackage.OP_GET_MODIFY));
} catch (Exception e){
Expand All @@ -136,35 +137,47 @@ public void run() {
private void localToRemote(){
Log.d(TAG,"Local to Remote");
reset();
UserClient userClient;
WebPackage bundle;

userClient = new UserClient();
bundle = new WebPackage();
bundle.setItem(WebPackage.DATA_OP,WebPackage.OP_UPDATE_MODIFY);
bundle.setItem(WebPackage.DATA_ID,MainActivity.user.getUserId());
bundle.setItem(WebPackage.DATA_EXTRA,String.valueOf(MainActivity.history.getModifyTime()));
userClient.setWebPackage(bundle);
userClient.start();
UserClient userClient1 = new UserClient();
WebPackage bundle1 = new WebPackage();
bundle1.setItem(WebPackage.DATA_OP,WebPackage.OP_UPDATE_MODIFY);
bundle1.setItem(WebPackage.DATA_ID,MainActivity.user.getUserId());
bundle1.setItem(WebPackage.DATA_EXTRA,String.valueOf(MainActivity.history.getModifyTime()));
userClient1.setWebPackage(bundle1);
userClient1.start();

GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();

userClient = new UserClient();
bundle = new WebPackage();
bundle.setItem(WebPackage.DATA_OP,WebPackage.OP_UPDATE_HISTORY);
bundle.setItem(WebPackage.DATA_ID,MainActivity.user.getUserId());
bundle.setItem(WebPackage.DATA_EXTRA,String.valueOf(gson.toJson(MainActivity.history,RecordHandler.class)));
userClient.setWebPackage(bundle);
userClient.start();
UserClient userClient2 = new UserClient();
WebPackage bundle2 = new WebPackage();
bundle2.setItem(WebPackage.DATA_OP,WebPackage.OP_UPDATE_HISTORY);
bundle2.setItem(WebPackage.DATA_ID,MainActivity.user.getUserId());
bundle2.setItem(WebPackage.DATA_EXTRA,String.valueOf(gson.toJson(MainActivity.history,RecordHandler.class)));
userClient2.setWebPackage(bundle2);
userClient2.start();

userClient = new UserClient();
bundle = new WebPackage();
bundle.setItem(WebPackage.DATA_OP,WebPackage.OP_UPDATE_FAVORITE);
bundle.setItem(WebPackage.DATA_ID,MainActivity.user.getUserId());
bundle.setItem(WebPackage.DATA_EXTRA,String.valueOf(gson.toJson(MainActivity.favorite,RecordHandler.class)));
userClient.setWebPackage(bundle);
userClient.start();
UserClient userClient3 = new UserClient();
WebPackage bundle3 = new WebPackage();
bundle3.setItem(WebPackage.DATA_OP,WebPackage.OP_UPDATE_FAVORITE);
bundle3.setItem(WebPackage.DATA_ID,MainActivity.user.getUserId());
bundle3.setItem(WebPackage.DATA_EXTRA,String.valueOf(gson.toJson(MainActivity.favorite,RecordHandler.class)));
userClient3.setWebPackage(bundle3);
userClient3.start();

Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
lost = true;
}
},waitTime);

while (!lost && (userClient1.getResponse() == null || userClient2.getResponse() == null
|| userClient3.getResponse() == null)){

}
Log.d(TAG,"Finished Update.");
}

private void remoteToLocal(){
Expand All @@ -176,44 +189,50 @@ private void remoteToLocal(){
hisBundle.setItem(WebPackage.DATA_OP,WebPackage.OP_GET_HISTORY);
hisBundle.setItem(WebPackage.DATA_ID,MainActivity.user.getUserId());
hisClient.setWebPackage(hisBundle);
hisClient.run();
hisClient.start();


UserClient favClient = new UserClient();
WebPackage favBundle = new WebPackage();
favBundle.setItem(WebPackage.DATA_OP,WebPackage.OP_GET_FAVORITE);
favBundle.setItem(WebPackage.DATA_ID,MainActivity.user.getUserId());
favClient.setWebPackage(favBundle);
favClient.run();
favClient.start();

Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
lost = true;
}
}, 3000);
}, 2*waitTime);
try {
while (!lost && (hisClient.getResponse() == null || favClient.getResponse() == null) ) {
wait();

}
if(lost) throw new Exception();
Log.d(TAG,"Get Client");
String hisValue = hisClient.getResponse().getString(WebPackage.OP_GET_HISTORY);
String favValue = hisClient.getResponse().getString(WebPackage.OP_GET_FAVORITE);
String favValue = favClient.getResponse().getString(WebPackage.OP_GET_FAVORITE);
Log.d(TAG,hisValue);
Log.d(TAG,favValue);
if(hisValue != null) {
MainActivity.history = gson.fromJson(hisValue, RecordHandler.class);
MainActivity.history.save();
}else {
MainActivity.history = new RecordHandler("history");
MainActivity.history.save();
}
Log.d(TAG,"History Pull Finished.");
if(favValue != null) {
MainActivity.favorite = gson.fromJson(favValue, RecordHandler.class);
MainActivity.favorite.save();
} else {
MainActivity.history = new RecordHandler("history");
MainActivity.history.save();
}
Log.d(TAG,"History Pull Finished.");
Log.d(TAG,"Remote Pull Finished.");
return ;
} catch (Exception e){
e.printStackTrace();
Expand All @@ -224,22 +243,22 @@ public void run() {
public void remoteUpdate(LoggedInUser user){
if (user.getUserId().equals("default"))
return;
Log.d(TAG,"REMOTEORLOCAL");
// 时间戳判断,决定本地与远程的同步关系
long remote = getModify();
long local = MainActivity.history.getModifyTime();
Log.d("RemoteTime:",String.valueOf(remote));
Log.d("LocalTime",String.valueOf(local));
if(local < remote){
if(local <= remote){
remoteToLocal();
}else if(local > remote){
}else {
localToRemote();
}else{
return;
}
}/**/
}

public void updateUser(final String username){
Log.d(TAG,"Update User"+username);
if(username.equals(MainActivity.user.getUserId())) return;
MainActivity.history.save();
MainActivity.favorite.save();
MainActivity.user = new LoggedInUser(username);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ public void onClick(View v) {
logoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Log Out","Begin");
new LoginDataSource().logout();
onDestroy();
finish();
}
});
}
Expand Down
11 changes: 1 addition & 10 deletions app/src/main/java/com/example/topnews/utils/RecordHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
import java.util.Vector;

public class RecordHandler {
Vector<String> records = new Vector<>();
public Vector<String> records = new Vector<>();
private final static String TAG = "RecordHandler";
private final String home = "/data/user/0/com.example.topnews/cache/";
private String type;
private File recordFile;
private boolean modify = false;
private boolean initial = true;

public RecordHandler(final String type){
modify = true;
String thome = home + MainActivity.user.getUserId() + "/";
File homedir = new File(thome);
if(!homedir.exists()) homedir.mkdirs();
Expand All @@ -37,7 +35,6 @@ public RecordHandler(final String type){
}

public void updateUser(){
modify = true;
records.clear();
String thome = home + MainActivity.user.getUserId() + "/";
File homedir = new File(thome);
Expand All @@ -49,25 +46,21 @@ public void add(final String newsID){
if(records.contains(newsID)) return;
// Log.d("Record Add", newsID);
records.add(newsID);
modify = true;
}

public void remove(final String newsID){
if(records.contains(newsID)) records.remove(newsID);
}
public void save(){
if(!modify) return;
try {
Log.d(TAG,"Save"+recordFile.getAbsolutePath());
recordFile.delete();
recordFile.createNewFile();
BufferedWriter writer = new BufferedWriter(new FileWriter(recordFile));
for (String value: records){
// Log.d("Record Save.",value);
writer.write( value + "\n");
}
writer.close();
modify = false;
initial = false;
} catch (Exception e){
e.printStackTrace();
Expand All @@ -78,7 +71,6 @@ public void load(){
if(!recordFile.exists()){
// Log.d("Record Error","File Missing.");
records.clear();
modify = true;
return;
}
Log.d(TAG,"Load"+recordFile.getAbsolutePath());
Expand All @@ -89,7 +81,6 @@ public void load(){
// Log.d("Record Load.",records.lastElement());
}
reader.close();
modify = true;
}catch (Exception e){
e.printStackTrace();
}
Expand Down

0 comments on commit 6ab8c23

Please sign in to comment.