├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── drawable
│ │ │ │ ├── background_sticky.9.png
│ │ │ │ ├── ic_build.xml
│ │ │ │ ├── ic_refresh.xml
│ │ │ │ ├── ic_delete_forever.xml
│ │ │ │ └── ic_launcher_background.xml
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── anim
│ │ │ │ ├── push_bottom_in.xml
│ │ │ │ └── push_bottom_out.xml
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── menu
│ │ │ │ ├── menu_detail.xml
│ │ │ │ └── menu_main.xml
│ │ │ ├── values
│ │ │ │ ├── colors.xml
│ │ │ │ ├── styles.xml
│ │ │ │ └── strings.xml
│ │ │ ├── layout
│ │ │ │ ├── dialog_connect_to.xml
│ │ │ │ ├── sticky_overview.xml
│ │ │ │ ├── activity_overview.xml
│ │ │ │ └── activity_detail.xml
│ │ │ └── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ ├── java
│ │ │ ├── bistu
│ │ │ │ └── share
│ │ │ │ │ ├── Instruction.java
│ │ │ │ │ ├── Detail.java
│ │ │ │ │ └── Overview.java
│ │ │ └── com
│ │ │ │ └── example
│ │ │ │ └── sticky
│ │ │ │ ├── DetailActivity
│ │ │ │ ├── HandlerDetailActivity.java
│ │ │ │ └── DetailActivity.java
│ │ │ │ ├── Overview
│ │ │ │ ├── StickyOverviewAdapter.java
│ │ │ │ └── OverviewActivity.java
│ │ │ │ └── client
│ │ │ │ └── Client.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── sticky
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── sticky
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .idea
├── vcs.xml
├── misc.xml
├── runConfigurations.xml
├── gradle.xml
└── codeStyles
│ └── Project.xml
├── .gitignore
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 | rootProject.name='sticky'
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leafee98/sticky_android_APP/master/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leafee98/sticky_android_APP/master/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leafee98/sticky_android_APP/master/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leafee98/sticky_android_APP/master/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leafee98/sticky_android_APP/master/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/background_sticky.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leafee98/sticky_android_APP/master/app/src/main/res/drawable/background_sticky.9.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leafee98/sticky_android_APP/master/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leafee98/sticky_android_APP/master/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leafee98/sticky_android_APP/master/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leafee98/sticky_android_APP/master/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leafee98/sticky_android_APP/master/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/leafee98/sticky_android_APP/master/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 |
--------------------------------------------------------------------------------
/app/src/main/res/anim/push_bottom_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/anim/push_bottom_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
8 |
9 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Dec 05 10:18:45 CST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_detail.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/java/bistu/share/Instruction.java:
--------------------------------------------------------------------------------
1 | package bistu.share;
2 |
3 | public class Instruction {
4 | public static final int SERVE_END = 999;
5 | public static final int GET_LIST = 1;
6 | public static final int GET_DETAIL = 2;
7 | public static final int UPDATE_STICKY = 3;
8 | public static final int REMOVE_STICKY = 4;
9 | public static final int ADD_STICKY = 5;
10 |
11 | public static final int ERR_CODE = -1;
12 | public static final int FINE_CODE = 0;
13 | }
--------------------------------------------------------------------------------
/app/src/test/java/com/example/sticky/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.example.sticky;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_build.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_refresh.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_delete_forever.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 | #000000
8 | #8F8E8E
9 | #FFF2AB
10 | #FFEB81
11 |
12 | #F8BFC2C2
13 |
14 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/dialog_connect_to.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
16 |
17 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/example/sticky/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.example.sticky;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.test.platform.app.InstrumentationRegistry;
6 | import androidx.test.ext.junit.runners.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24 |
25 | assertEquals("com.example.sticky", appContext.getPackageName());
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | sticky
3 | refresh list
4 | connect
5 | add sticky
6 | sticky content
7 | sticky modify time
8 | server address. eg: 1.2.3.4:1234
9 | Connect to
10 | delete
11 | sticky content here…
12 |
13 |
14 | long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/java/bistu/share/Detail.java:
--------------------------------------------------------------------------------
1 | package bistu.share;
2 |
3 | import java.io.Serializable;
4 | import java.sql.Timestamp;
5 |
6 | public class Detail implements Serializable {
7 | private long id;
8 | private Timestamp modifyTime;
9 | private String fullText;
10 |
11 | public Detail(long id, Timestamp modifyTime, String fullText) {
12 | this.id = id;
13 | this.modifyTime = modifyTime;
14 | this.fullText = fullText;
15 | }
16 |
17 | public long getId() { return id; }
18 | public void setId(long id) { this.id = id; }
19 |
20 | public Timestamp getModifyTime() { return modifyTime; }
21 | public void setModifyTime(Timestamp modifyTime) { this.modifyTime = modifyTime; }
22 |
23 | public String getFullText() { return fullText; }
24 | public void setFullText(String fullText) { this.fullText = fullText; }
25 |
26 | @Override
27 | public String toString() {
28 | return String.format("Detail: id=%d, modifyTime=%s, fullText=%s", id, modifyTime.toString(), fullText);
29 | }
30 |
31 | }
--------------------------------------------------------------------------------
/app/src/main/java/bistu/share/Overview.java:
--------------------------------------------------------------------------------
1 | package bistu.share;
2 |
3 | import java.io.Serializable;
4 | import java.sql.Timestamp;
5 |
6 | public class Overview implements Serializable {
7 | private long id;
8 | private Timestamp modifyTime;
9 | private String summary;
10 |
11 | public Overview(long id, Timestamp modifyTime, String summary) {
12 | this.id = id;
13 | this.modifyTime = modifyTime;
14 | this.summary = summary;
15 | }
16 |
17 | public long getId() { return id; }
18 | public void setId(long id) { this.id = id; }
19 |
20 | public Timestamp getModifyTime() { return modifyTime; }
21 | public void setModifyTime(Timestamp modifyTime) { this.modifyTime = modifyTime; }
22 |
23 | public String getSummary() { return summary; }
24 | public void setSummary(String summary) { this.summary = summary; }
25 |
26 | @Override
27 | public String toString() {
28 | return String.format("Overview: id=%ld, modifyTime=%s, summary=%s", id, modifyTime.toString(), summary);
29 | }
30 |
31 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
14 |
17 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 29
5 | buildToolsVersion "29.0.2"
6 | defaultConfig {
7 | applicationId "com.example.sticky"
8 | minSdkVersion 27
9 | targetSdkVersion 29
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | compileOptions {
21 | sourceCompatibility = 1.8
22 | targetCompatibility = 1.8
23 | }
24 | }
25 |
26 | dependencies {
27 | implementation fileTree(dir: 'libs', include: ['*.jar'])
28 | implementation 'androidx.appcompat:appcompat:1.1.0'
29 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
30 | testImplementation 'junit:junit:4.12'
31 | androidTestImplementation 'androidx.test:runner:1.2.0'
32 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
33 | implementation 'androidx.recyclerview:recyclerview:1.1.0'
34 | }
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/sticky/DetailActivity/HandlerDetailActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.sticky.DetailActivity;
2 |
3 | import android.os.Handler;
4 | import android.os.Message;
5 |
6 | import androidx.annotation.NonNull;
7 |
8 | import bistu.share.Detail;
9 |
10 | import java.util.Date;
11 |
12 | class HandlerDetailActivity extends Handler {
13 |
14 | private DetailActivity activity;
15 |
16 | static final int HIDE_PROGRESS_BAR = 1;
17 | static final int SHOW_PROGRESS_SAVING = 2;
18 | static final int HIDE_PROGRESS_SAVING = 3;
19 | static final int ASSIGN_DETAIL = 4;
20 | static final int UPDATE_MODIFY = 5;
21 | static final int FINISH_ACTIVITY = 9;
22 |
23 | HandlerDetailActivity(DetailActivity activity) {
24 | this.activity = activity;
25 | }
26 |
27 | @Override
28 | public void handleMessage(@NonNull Message msg) {
29 | super.handleMessage(msg);
30 | switch (msg.what) {
31 | case HIDE_PROGRESS_BAR: this.activity.showProgressLoading(false); break;
32 | case SHOW_PROGRESS_SAVING: this.activity.showProgressSaving(true); break;
33 | case HIDE_PROGRESS_SAVING: this.activity.showProgressSaving(false); break;
34 | case ASSIGN_DETAIL: this.activity.assignDetail((Detail) msg.obj); break;
35 | case UPDATE_MODIFY: this.activity.updateModifyTime((Date) msg.obj); break;
36 | case FINISH_ACTIVITY: this.activity.finish(); break;
37 | }
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/sticky_overview.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
23 |
24 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/sticky/Overview/StickyOverviewAdapter.java:
--------------------------------------------------------------------------------
1 | package com.example.sticky.Overview;
2 |
3 | import android.view.LayoutInflater;
4 | import android.view.View;
5 | import android.view.ViewGroup;
6 | import android.widget.TextView;
7 |
8 | import androidx.annotation.NonNull;
9 | import androidx.recyclerview.widget.RecyclerView;
10 |
11 | import com.example.sticky.R;
12 | import bistu.share.Overview;
13 |
14 | import java.util.List;
15 |
16 | public class StickyOverviewAdapter extends RecyclerView.Adapter {
17 |
18 | class ViewHolder extends RecyclerView.ViewHolder {
19 | TextView content, modify;
20 | View wholeView;
21 |
22 | ViewHolder(@NonNull View itemView) {
23 | super(itemView);
24 | this.wholeView = itemView;
25 | this.content = itemView.findViewById(R.id.textView_sticky_overview_content);
26 | this.modify = itemView.findViewById(R.id.textView_sticky_overview_modify);
27 | }
28 | }
29 |
30 | private List overviewList;
31 | private OverviewActivity activity;
32 |
33 | StickyOverviewAdapter(OverviewActivity activity, List overviewList) {
34 | this.activity = activity;
35 | this.overviewList = overviewList;
36 | }
37 |
38 | @NonNull
39 | @Override
40 | public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
41 | View v = LayoutInflater.from(parent.getContext())
42 | .inflate(R.layout.sticky_overview, parent, false);
43 | return new ViewHolder(v);
44 | }
45 |
46 | @Override
47 | public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
48 | Overview o = this.overviewList.get(position);
49 | holder.content.setText(o.getSummary());
50 | holder.modify.setText(o.getModifyTime().toString());
51 |
52 | holder.wholeView.setOnClickListener((View v) ->
53 | activity.viewDetail(this.overviewList.get(holder.getAdapterPosition()).getId())
54 | );
55 | }
56 |
57 | @Override
58 | public int getItemCount() {
59 | return this.overviewList.size();
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_overview.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
17 |
18 |
30 |
31 |
41 |
42 |
50 |
51 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | xmlns:android
14 |
15 | ^$
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | xmlns:.*
25 |
26 | ^$
27 |
28 |
29 | BY_NAME
30 |
31 |
32 |
33 |
34 |
35 |
36 | .*:id
37 |
38 | http://schemas.android.com/apk/res/android
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 | .*:name
48 |
49 | http://schemas.android.com/apk/res/android
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | name
59 |
60 | ^$
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 | style
70 |
71 | ^$
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 | .*
81 |
82 | ^$
83 |
84 |
85 | BY_NAME
86 |
87 |
88 |
89 |
90 |
91 |
92 | .*
93 |
94 | http://schemas.android.com/apk/res/android
95 |
96 |
97 | ANDROID_ATTRIBUTE_ORDER
98 |
99 |
100 |
101 |
102 |
103 |
104 | .*
105 |
106 | .*
107 |
108 |
109 | BY_NAME
110 |
111 |
112 |
113 |
114 |
115 |
116 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_detail.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
17 |
18 |
37 |
38 |
51 |
52 |
65 |
66 |
76 |
77 |
86 |
87 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/sticky/client/Client.java:
--------------------------------------------------------------------------------
1 | package com.example.sticky.client;
2 |
3 | import android.util.Log;
4 |
5 | import bistu.share.Detail;
6 | import bistu.share.Instruction;
7 | import bistu.share.Overview;
8 |
9 | import java.io.IOException;
10 | import java.io.ObjectInputStream;
11 | import java.io.ObjectOutputStream;
12 | import java.io.Serializable;
13 | import java.net.InetAddress;
14 | import java.net.Socket;
15 | import java.util.List;
16 |
17 | public class Client implements Serializable {
18 |
19 | private static Client client;
20 | private static InetAddress address;
21 | private static int port;
22 |
23 | private Socket socket;
24 | private ObjectInputStream objectIn;
25 | private ObjectOutputStream objectOut;
26 |
27 | public static Client getInstance() {
28 | return client;
29 | }
30 | public static boolean init(InetAddress host, int p) {
31 | try {
32 | client = new Client(host, p);
33 | address = host;
34 | port = p;
35 | } catch (IOException e) {
36 | Log.e(Client.class.getName(), "fail to init client.");
37 | return false;
38 | }
39 | return true;
40 | }
41 |
42 | private Client(InetAddress host, int port) throws IOException {
43 | try {
44 | this.socket = new Socket(host, port);
45 | this.objectOut = new ObjectOutputStream(this.socket.getOutputStream());
46 | this.objectIn = new ObjectInputStream(this.socket.getInputStream());
47 | Log.i(Client.class.getName(), "object stream initialized.");
48 | } catch (IOException e) {
49 | Log.e(Client.class.getName(), "IO error occurred while initializing object stream", e);
50 | throw e;
51 | }
52 | }
53 |
54 | /**
55 | * send SERVE_END sign to server. close object stream and socket.
56 | */
57 | public void shutdown() {
58 | try {
59 | objectOut.writeInt(Instruction.SERVE_END);
60 | this.objectIn.close();
61 | this.objectOut.close();
62 | this.socket.close();
63 | client = null;
64 | } catch (IOException e) {
65 | Log.e(this.getClass().getName(), "IO error occurred while ending serve.", e);
66 | }
67 | }
68 |
69 | /**
70 | * get sticky overview list.
71 | * @return list of overview. null if any error occurred.
72 | */
73 | public List getList() {
74 | return this.getList(true);
75 | }
76 |
77 | private List getList(boolean retry) {
78 | List list = null;
79 | try {
80 | objectOut.writeInt(Instruction.GET_LIST);
81 | objectOut.flush();
82 |
83 | if (objectIn.readInt() == Instruction.FINE_CODE) {
84 | Object o = objectIn.readObject();
85 | list = (List) o;
86 | } else {
87 | Log.e(this.getClass().getName(), "Server error while getting List.");
88 | }
89 | } catch (IOException | ClassNotFoundException e) {
90 | Log.e(this.getClass().getName(), "IO error occurred while getting list.", e);
91 | if (retry && reconnect())
92 | return this.getList(false);
93 | }
94 | return list;
95 | }
96 |
97 | /**
98 | * get the detail of sticky specified by id.
99 | * @param id the sticky's id
100 | * @return detail of sticky, null if any error occurred.
101 | */
102 | public Detail getDetail(long id) {
103 | return this.getDetail(true, id);
104 | }
105 |
106 | private Detail getDetail(boolean retry, long id) {
107 | Detail d = null;
108 | try {
109 | objectOut.writeInt(Instruction.GET_DETAIL);
110 | objectOut.writeLong(id);
111 | objectOut.flush();
112 |
113 | if (objectIn.readInt() == Instruction.FINE_CODE)
114 | d = (Detail)objectIn.readObject();
115 | else
116 | Log.e(this.getClass().getName(), "Server error while getting Detail.");
117 | } catch (IOException | ClassNotFoundException e) {
118 | Log.e(this.getClass().getName(), "IO error occurred while getting detail.", e);
119 | if (retry && reconnect())
120 | return this.getDetail(false, id);
121 | }
122 | return d;
123 | }
124 |
125 | /**
126 | * add a new sticky.
127 | * @return the id of new sticky. -1 if any error occurred.
128 | */
129 | public long addSticky() {
130 | return this.addSticky(true);
131 | }
132 |
133 | private long addSticky(boolean retry) {
134 | long id = -1;
135 | try {
136 | objectOut.writeInt(Instruction.ADD_STICKY);
137 | objectOut.flush();
138 |
139 | if(objectIn.readInt() == Instruction.FINE_CODE)
140 | id = objectIn.readLong();
141 | else
142 | Log.e(this.getClass().getName(), "Server error while adding sticky.");
143 | } catch (IOException e) {
144 | Log.e(this.getClass().getName(), "IO error occurred while adding sticky.", e);
145 | if (retry && reconnect())
146 | return this.addSticky(false);
147 | }
148 | return id;
149 | }
150 |
151 | /**
152 | * update a sticky.
153 | * @param d the sticky with new content.
154 | * @return true if succeed, false if any error occurred.
155 | */
156 | public boolean updateSticky(Detail d) {
157 | return this.updateSticky(true, d);
158 | }
159 |
160 | private boolean updateSticky(boolean retry, Detail d) {
161 | boolean result = false;
162 | try {
163 | Log.i(this.getClass().getName(), String.format("updating, %s", d.toString()));
164 | objectOut.writeInt(Instruction.UPDATE_STICKY);
165 | objectOut.writeUnshared(d);
166 | objectOut.flush();
167 |
168 | if (objectIn.readInt() == Instruction.FINE_CODE) {
169 | result = true;
170 | Log.i(this.getClass().getName(), String.format("success update, %s", d.toString()));
171 | } else
172 | Log.e(this.getClass().getName(), "Server error while updating sticky.");
173 | } catch (IOException e) {
174 | Log.e(this.getClass().getName(), "IO error occurred while updating sticky", e);
175 | if (retry && reconnect())
176 | return this.updateSticky(false, d);
177 | }
178 | return result;
179 | }
180 |
181 | /**
182 | * remove a sticky specified by id
183 | * @param id the id of sticky
184 | * @return true if succeed, false if any error occurred.
185 | */
186 | public boolean removeStick(long id) {
187 | return this.removeStick(true, id);
188 | }
189 |
190 | private boolean removeStick(boolean retry, long id) {
191 | boolean result = false;
192 | try {
193 | objectOut.writeInt(Instruction.REMOVE_STICKY);
194 | objectOut.writeLong(id);
195 | objectOut.flush();
196 |
197 | if (objectIn.readInt() == Instruction.FINE_CODE)
198 | result = true;
199 | else
200 | Log.e(this.getClass().getName(), "Server error while removing sticky.");
201 | } catch (IOException e) {
202 | Log.e(this.getClass().getName(), "IO error occurred while removing sticky.", e);
203 | if (retry && reconnect())
204 | return this.removeStick(false, id);
205 | }
206 | return result;
207 | }
208 |
209 | private boolean reconnect() {
210 | int i = 0;
211 | while (!init(address, port) && i++ < 3) {
212 | Log.e(this.getClass().getName(), String.format("reconnect fail. retry %d time.", 3));
213 | }
214 | if (i >= 3) {
215 | Log.e(this.getClass().getName(), "reconnect failed after 3 times try.");
216 | return false;
217 | } else {
218 | Log.i(this.getClass().getName(), "reconnected, retry doing the operate.");
219 | return true;
220 | }
221 | }
222 |
223 | }
224 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/sticky/DetailActivity/DetailActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.sticky.DetailActivity;
2 |
3 | import androidx.annotation.NonNull;
4 | import androidx.appcompat.app.AppCompatActivity;
5 | import androidx.appcompat.widget.Toolbar;
6 |
7 | import android.content.Intent;
8 | import android.content.res.Configuration;
9 | import android.os.Bundle;
10 | import android.os.Handler;
11 | import android.os.Message;
12 | import android.util.Log;
13 | import android.view.Menu;
14 | import android.view.MenuItem;
15 | import android.view.View;
16 | import android.widget.EditText;
17 | import android.widget.ProgressBar;
18 | import android.widget.TextView;
19 | import android.widget.Toast;
20 |
21 | import com.example.sticky.R;
22 | import com.example.sticky.client.Client;
23 | import bistu.share.Detail;
24 |
25 | import java.sql.Timestamp;
26 | import java.util.Date;
27 |
28 | public class DetailActivity extends AppCompatActivity {
29 |
30 | private Handler handler;
31 | private Thread stickyUpdater;
32 | private boolean clickable = true;
33 |
34 | private Detail detail;
35 |
36 | private EditText stickyContent;
37 | private TextView stickyModify;
38 | private ProgressBar progressLoading;
39 | private ProgressBar progressSaving;
40 |
41 | @Override
42 | protected void onCreate(Bundle savedInstanceState) {
43 | super.onCreate(savedInstanceState);
44 | setContentView(R.layout.activity_detail);
45 |
46 | this.handler = new HandlerDetailActivity(this);
47 | this.assignView();
48 |
49 | long id = this.getVariable();
50 | this.requestDetail(id);
51 |
52 | Toolbar toolbar = findViewById(R.id.toolbar_detail);
53 | super.setSupportActionBar(toolbar);
54 | }
55 |
56 | @Override
57 | public boolean onCreateOptionsMenu(Menu menu) {
58 | getMenuInflater().inflate(R.menu.menu_detail, menu);
59 | return true;
60 | }
61 |
62 | @Override
63 | public boolean onOptionsItemSelected(@NonNull MenuItem item) {
64 | if(clickable)
65 | this.removeSticky(detail.getId());
66 | return true;
67 | }
68 |
69 | @Override
70 | public void onConfigurationChanged(@NonNull Configuration newConfig) { }
71 |
72 | /**
73 | * set content with data come from @param d.
74 | * Will init updater once be called.
75 | * @param d the Detail object.
76 | */
77 | void assignDetail(Detail d) {
78 | this.detail = d;
79 | this.stickyContent.setText(d.getFullText());
80 | this.stickyModify.setText(d.getModifyTime().toString());
81 | this.initStickyUpdater();
82 | }
83 |
84 | void showProgressLoading(boolean show) {
85 | if (show) {
86 | this.progressLoading.setVisibility(View.VISIBLE);
87 | this.clickable = false;
88 | } else {
89 | this.progressLoading.setVisibility(View.GONE);
90 | this.clickable = true;
91 | }
92 | }
93 |
94 | void showProgressSaving(boolean show) {
95 | if (show) {
96 | this.progressSaving.setVisibility(View.VISIBLE);
97 | } else {
98 | this.progressSaving.setVisibility(View.GONE);
99 | }
100 | }
101 |
102 | void updateModifyTime(Date d) {
103 | this.stickyModify.setText(d.toString());
104 | }
105 |
106 | /**
107 | * Get content from textView and update sticky by client every 3 seconds.
108 | * Will update the last time once interrupted.
109 | */
110 | private void initStickyUpdater() {
111 | this.stickyUpdater = new Thread(() -> {
112 | try {
113 | while (true) {
114 | Thread.sleep(3000);
115 |
116 | String newContent = this.stickyContent.getText().toString();
117 | if (!newContent.equals(this.detail.getFullText())) {
118 | Message msg1 = new Message();
119 | Message msg2 = new Message();
120 | Message msg3 = new Message();
121 | msg1.what = HandlerDetailActivity.SHOW_PROGRESS_SAVING;
122 | msg2.what = HandlerDetailActivity.HIDE_PROGRESS_SAVING;
123 |
124 | this.handler.sendMessage(msg1);
125 |
126 | Date d = new Date();
127 | this.detail.setFullText(newContent);
128 | this.detail.setModifyTime(new Timestamp(d.getTime()));
129 | Log.i(DetailActivity.class.getName(), String.format("Detail: %s", this.detail.toString()));
130 |
131 | if (Client.getInstance().updateSticky(this.detail))
132 | Log.i(DetailActivity.class.getName(), "updated Detail.");
133 | else
134 | runOnUiThread(() ->
135 | Toast.makeText(this, "fail to update sticky. (auto update every 3 sec)",
136 | Toast.LENGTH_SHORT).show());
137 |
138 |
139 | msg3.what = HandlerDetailActivity.UPDATE_MODIFY;
140 | msg3.obj = d;
141 | this.handler.sendMessage(msg3);
142 | this.handler.sendMessage(msg2);
143 | }
144 | }
145 | } catch (InterruptedException e) {
146 | Log.w(DetailActivity.class.getName(), "updater interrupted.");
147 |
148 | String newContent = this.stickyContent.getText().toString();
149 | if (!newContent.equals(this.detail.getFullText())) {
150 | Log.i(DetailActivity.class.getName(), "updating Detail.");
151 | Date d = new Date();
152 | this.detail.setFullText(newContent);
153 | this.detail.setModifyTime(new Timestamp(d.getTime()));
154 |
155 | if (Client.getInstance().updateSticky(this.detail))
156 | Log.i(DetailActivity.class.getName(), "updated Detail.");
157 | else
158 | runOnUiThread(() ->
159 | Toast.makeText(this, "fail to update sticky on activity finish.",
160 | Toast.LENGTH_SHORT).show());
161 |
162 | Log.i(DetailActivity.class.getName(), "updated Detail.");
163 | }
164 | }
165 | });
166 | this.stickyUpdater.start();
167 | }
168 |
169 | /**
170 | * Request Detail by client (network request).
171 | * Will call this.assignDetail() by handler;
172 | * @param id the if of Detail requested.
173 | */
174 | private void requestDetail(long id) {
175 | this.showProgressLoading(true);
176 | Log.i(DetailActivity.class.getName(), String.format("requiring Detail, id=%d", id));
177 | new Thread(() -> {
178 | Detail d = Client.getInstance().getDetail(id);
179 | if (d != null) {
180 | Message msg1 = new Message();
181 | msg1.what = HandlerDetailActivity.ASSIGN_DETAIL;
182 | msg1.obj = d;
183 | handler.sendMessage(msg1);
184 | } else {
185 | Toast.makeText(this, "fail to get detail of sticky.", Toast.LENGTH_SHORT).show();
186 | }
187 |
188 | Message msg2 = new Message();
189 | msg2.what = HandlerDetailActivity.HIDE_PROGRESS_BAR;
190 | handler.sendMessage(msg2);
191 | }).start();
192 | }
193 |
194 | /**
195 | * assign this.client from intent
196 | * @return the id gotten from intent.
197 | */
198 | private long getVariable() {
199 | Intent intent = getIntent();
200 | return intent.getLongExtra("id", -1);
201 | }
202 |
203 | private void assignView() {
204 | this.stickyContent = this.findViewById(R.id.editText_sticky_detail_content);
205 | this.stickyModify = this.findViewById(R.id.textView_sticky_detail_modify);
206 | this.progressLoading = this.findViewById(R.id.progressBar_loading_detail);
207 | this.progressSaving = this.findViewById(R.id.progressBar_saving_detail);
208 | }
209 |
210 | /**
211 | * showProgressLoading -> remove sticky on server (network request)
212 | * -> finish activity.
213 | * @param id the id of sticky which will be removed.
214 | */
215 | private void removeSticky(long id) {
216 | this.showProgressLoading(true);
217 | new Thread(() -> {
218 | if (!Client.getInstance().removeStick(id))
219 | Toast.makeText(this, "fail to remove sticky due to network error.",
220 | Toast.LENGTH_SHORT).show();
221 |
222 | Message msg1 = new Message();
223 | Message msg2 = new Message();
224 | msg1.what = HandlerDetailActivity.HIDE_PROGRESS_BAR;
225 | msg2.what = HandlerDetailActivity.FINISH_ACTIVITY;
226 | handler.sendMessage(msg1);
227 | handler.sendMessage(msg2);
228 | }).start();
229 | }
230 |
231 | @Override
232 | protected void onDestroy() {
233 | Log.w(DetailActivity.class.getName(), "Activity destroyed.");
234 | this.stickyUpdater.interrupt();
235 | super.onDestroy();
236 | }
237 | }
238 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/sticky/Overview/OverviewActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.sticky.Overview;
2 |
3 | import androidx.annotation.NonNull;
4 | import androidx.appcompat.app.AppCompatActivity;
5 | import androidx.appcompat.widget.Toolbar;
6 | import androidx.recyclerview.widget.LinearLayoutManager;
7 | import androidx.recyclerview.widget.RecyclerView;
8 |
9 | import android.app.AlertDialog;
10 | import android.content.DialogInterface;
11 | import android.content.Intent;
12 | import android.content.SharedPreferences;
13 | import android.content.res.Configuration;
14 | import android.os.Bundle;
15 | import android.util.Log;
16 | import android.view.Menu;
17 | import android.view.MenuInflater;
18 | import android.view.MenuItem;
19 | import android.view.View;
20 | import android.widget.Button;
21 | import android.widget.EditText;
22 | import android.widget.ProgressBar;
23 | import android.widget.Toast;
24 |
25 | import com.example.sticky.DetailActivity.DetailActivity;
26 | import com.example.sticky.R;
27 | import com.example.sticky.client.Client;
28 | import bistu.share.Overview;
29 |
30 | import java.net.InetAddress;
31 | import java.net.UnknownHostException;
32 | import java.util.List;
33 |
34 | public class OverviewActivity extends AppCompatActivity {
35 |
36 | private Button addSticky;
37 | private RecyclerView recycler;
38 | private ProgressBar progress;
39 | private StickyOverviewAdapter adapter;
40 |
41 | private boolean startup = true;
42 | private boolean clickable = true;
43 |
44 | @Override
45 | protected void onCreate(Bundle savedInstanceState) {
46 | super.onCreate(savedInstanceState);
47 | setContentView(R.layout.activity_overview);
48 |
49 | Toolbar toolbar = findViewById(R.id.toolbar_overview);
50 | setSupportActionBar(toolbar);
51 |
52 | this.assignView();
53 | this.assignAction();
54 |
55 | // connect last connected server at startup.
56 | SharedPreferences sp = this.getSharedPreferences("serverAddress", MODE_PRIVATE);
57 | String address = sp.getString("address", null);
58 | if (address != null) {
59 | this.initClient(address);
60 | }else {
61 | this.showConfigureServerDialog();
62 | }
63 | }
64 |
65 | @Override
66 | public boolean onCreateOptionsMenu(Menu menu) {
67 | MenuInflater inflater = this.getMenuInflater();
68 | inflater.inflate(R.menu.menu_main, menu);
69 | return true;
70 | }
71 |
72 | @Override
73 | public boolean onOptionsItemSelected(@NonNull MenuItem item) {
74 | if (clickable) {
75 | switch (item.getItemId()) {
76 | case R.id.refresh_list:
77 | this.refreshOverviewList();
78 | break;
79 | case R.id.connect_configuration:
80 | this.showConfigureServerDialog();
81 | break;
82 | }
83 | }
84 | return true;
85 | }
86 |
87 | /**
88 | * refresh list while come back from detail.
89 | */
90 | @Override
91 | protected void onResume() {
92 | super.onResume();
93 | if (!this.startup)
94 | this.refreshOverviewList();
95 | else
96 | this.startup = false;
97 | }
98 |
99 | @Override
100 | protected void onDestroy() {
101 | new Thread(() -> Client.getInstance().shutdown()).start();
102 | super.onDestroy();
103 | }
104 |
105 | @Override
106 | public void onConfigurationChanged(@NonNull Configuration newConfig) { }
107 |
108 | /**
109 | * start DetailActivity to view sticky's detail.
110 | * @param id the id of sticky to view.
111 | */
112 | void viewDetail(long id) {
113 | if (clickable) {
114 | Intent instant = new Intent(this, DetailActivity.class);
115 | instant.putExtra("id", id);
116 | startActivity(instant);
117 | }
118 | }
119 |
120 | private void assignAction() {
121 | this.addSticky.setOnClickListener((View v) ->
122 | new Thread(() -> {
123 | if (clickable) {
124 | this.runOnUiThread(() -> this.showProgress(true));
125 | long id = Client.getInstance().addSticky();
126 | if (id > 0)
127 | this.runOnUiThread(() -> {
128 | this.showProgress(false);
129 | this.viewDetail(id);
130 | });
131 | else
132 | Toast.makeText(this, "unknown error occurred while adding sticky",
133 | Toast.LENGTH_SHORT).show();
134 | }
135 | }).start()
136 | );
137 | }
138 |
139 | private void assignView() {
140 | this.addSticky = this.findViewById(R.id.button_add_sticky);
141 | this.progress = this.findViewById(R.id.progressBar_loading_overview);
142 | this.recycler = this.findViewById(R.id.recycler_sticky_overview);
143 |
144 | this.recycler.setLayoutManager(new LinearLayoutManager(this));
145 | }
146 |
147 | private void initClient(String address) {
148 | this.showProgress(true);
149 | new Thread(() -> {
150 | try {
151 | Log.i("initClient()", String.format("Address: %s", address));
152 | int index = address.indexOf(':');
153 | if (index < 0)
154 | throw new NumberFormatException();
155 | String hostAddress = address.substring(0, index);
156 | int port = Integer.parseInt(address.substring(index + 1));
157 |
158 | if(Client.init(InetAddress.getByName(hostAddress), port)) {
159 | Log.i("initClient()", "connected to server.");
160 |
161 | // save address connected.
162 | SharedPreferences.Editor editor = this.getSharedPreferences("serverAddress", MODE_PRIVATE).edit();
163 | editor.putString("address", address);
164 | editor.apply();
165 |
166 | this.runOnUiThread(() -> {
167 | Toast.makeText(this, "connected to server.", Toast.LENGTH_SHORT).show();
168 | this.refreshOverviewList();
169 | });
170 | } else {
171 | this.runOnUiThread(() -> {
172 | Toast.makeText(this, "fail to connect to server due to unknown error.", Toast.LENGTH_SHORT).show();
173 | this.showConfigureServerDialog();
174 | });
175 | }
176 | } catch (NumberFormatException e) {
177 | this.runOnUiThread(() -> {
178 | Toast.makeText(this, "server address format error!", Toast.LENGTH_SHORT).show();
179 | this.showConfigureServerDialog();
180 | });
181 | } catch (UnknownHostException e) {
182 | this.runOnUiThread(() -> {
183 | Toast.makeText(this, "unable to resolve address!", Toast.LENGTH_SHORT).show();
184 | this.showConfigureServerDialog();
185 | });
186 | }
187 |
188 | this.runOnUiThread(() -> this.showProgress(false));
189 | }).start();
190 | }
191 |
192 | private void showProgress(boolean show) {
193 | if (show) {
194 | this.progress.setVisibility(View.VISIBLE);
195 | this.clickable = false;
196 | Log.i("progressBar", "show progress bar.");
197 | } else {
198 | this.progress.setVisibility(View.INVISIBLE);
199 | this.clickable = true;
200 | Log.i("progressBar", "hide progress bar.");
201 | }
202 | }
203 |
204 | private void refreshOverviewList() {
205 | this.showProgress(true);
206 | new Thread(() -> {
207 | List overviews = Client.getInstance().getList();
208 | if (overviews != null) {
209 | this.adapter = new StickyOverviewAdapter(this, overviews);
210 | this.runOnUiThread(() -> {
211 | this.recycler.setAdapter(adapter);
212 | this.showProgress(false);
213 | });
214 | } else {
215 | this.runOnUiThread(() -> {
216 | Toast.makeText(this, "disconnected from server due to network error.", Toast.LENGTH_SHORT).show();
217 | this.showProgress(false);
218 | });
219 | }
220 | }).start();
221 | }
222 |
223 | private void showConfigureServerDialog() {
224 | AlertDialog.Builder builder =new AlertDialog.Builder(this);
225 |
226 | View dialogLayout = View.inflate(this, R.layout.dialog_connect_to, null);
227 | builder.setView(dialogLayout);
228 | EditText serverAddress = dialogLayout.findViewById(R.id.editText_server_address);
229 |
230 | // set text on editText to be server last connected.
231 | SharedPreferences sp = getSharedPreferences("serverAddress", MODE_PRIVATE);
232 | serverAddress.setText(sp.getString("address", ""));
233 |
234 | builder.setTitle(R.string.dialog_title_connect_to);
235 | builder.setPositiveButton("OK", (DialogInterface dia, int which) ->
236 | this.initClient(serverAddress.getText().toString()));
237 | builder.setCancelable(false);
238 | builder.show();
239 | }
240 |
241 | }
242 |
--------------------------------------------------------------------------------