返回课程列表
29 | *
30 | * 只读取6行7列
31 | */
32 | public static List handleExcel(String path, int startRow, int startCol, HandleResult handleResult) {
33 | InputStream inputStream = null;
34 | List courseList = new ArrayList<>();
35 | //Log.d("filePath",path);
36 | Workbook excel = null;
37 | try {
38 | File file = new File(path);
39 |
40 | if (file.exists()) {
41 | inputStream = new FileInputStream(file);
42 | } else {
43 | //Log.d("file","do not exist");
44 | return courseList;
45 | }
46 | excel = Workbook.getWorkbook(inputStream);
47 | Sheet rs = excel.getSheet(0);
48 | int rowCount = 6;
49 | int weight = 2;
50 |
51 | Range[] ranges = rs.getMergedCells();
52 |
53 |
54 | if (startCol + 7 - 1 > rs.getColumns() || startRow + rowCount - 1 > rs.getRows()) {
55 | return courseList;
56 | }
57 |
58 | startCol -= 2;//rs.getCell以零开始
59 | startRow -= 2;
60 |
61 | for (int i = 1; i <= 7; i++) {
62 | for (int j = 1; j <= rowCount; j++) {
63 | Cell cell = rs.getCell(startCol + i, startRow + j);
64 | String str = handleCell(cell.getContents());
65 |
66 | int row_length = 1;
67 | for (Range range : ranges) {
68 | if (range.getTopLeft() == cell) {
69 | row_length = range.getBottomRight().getRow() - cell.getRow() + 1;
70 | break;
71 | }
72 | }
73 |
74 | if (!str.isEmpty()) {
75 |
76 | //一个格子有两个课程,分割处理
77 | String[] strings = str.split("\n\n");
78 |
79 | for (String s : strings) {
80 | Course course = handleResult.handle(s, j, i);
81 | if (course != null) {
82 | course.setClassLength(weight * row_length);
83 | courseList.add(course);
84 | }
85 | }
86 | }
87 | }
88 |
89 | }
90 | return courseList;
91 | } catch (BiffException | IOException e) {
92 | e.printStackTrace();
93 | return null;
94 | } finally {
95 | try {
96 | if (excel != null)
97 | excel.close();
98 | if (inputStream != null)
99 | inputStream.close();
100 | } catch (IOException e) {
101 | e.printStackTrace();
102 | }
103 | }
104 |
105 | }
106 |
107 | public static List handleExcel(String path, int startRow, int startCol) {
108 | return handleExcel(path, startRow, startCol, (courseStr, row, col) -> {
109 | Course course = getCourseFromString(courseStr);
110 | if (course == null)
111 | return null;
112 | course.setDayOfWeek(col);
113 | course.setClassStart(row * 2 - 1);
114 | return course;
115 | });
116 | }
117 |
118 | public static List handleExcel(String path) {
119 | return handleExcel(path, 2, 2);
120 | }
121 |
122 | /**
123 | * 从表格中的内容提取课程信息
124 | *
125 | * @param str String
126 | * @return Course
127 | */
128 | public static Course getCourseFromString(String str) {
129 |
130 | String[] contents = str.split("\n");
131 | if (contents.length < 4)
132 | return null;
133 | Course course = new Course();
134 | course.setName(contents[0]);
135 | course.setTeacher(contents[1]);
136 |
137 | course.setWeekOfTerm(getWeekOfTermFromString(contents[2]));
138 |
139 | course.setClassRoom(contents[3]);
140 |
141 | return course;
142 |
143 | }
144 |
145 | private static int getWeekOfTermFromString(String str) {
146 | //Log.d("excel",str);
147 | String[] s1 = str.split("\\[");
148 | String[] s11 = s1[0].split(",");
149 |
150 | int weekOfTerm = 0;
151 | for (String s : s11) {
152 | if (s == null || s.isEmpty())
153 | continue;
154 | if (s.contains("-")) {
155 | int space = 2;
156 | if (s1[1].equals("周]")) {
157 | space = 1;
158 | }
159 | String[] s2 = s.split("-");
160 | if (s2.length != 2) {
161 | System.out.println("error");
162 | return 0;
163 | }
164 | int p = Integer.parseInt(s2[0]);
165 | int q = Integer.parseInt(s2[1]);
166 |
167 | for (int n = p; n <= q; n += space) {
168 | weekOfTerm += 1 << (Config.getMaxWeekNum() - n);
169 | }
170 | } else {
171 | weekOfTerm += 1 << (Config.getMaxWeekNum() - Integer.parseInt(s));
172 | }
173 | }
174 | return weekOfTerm;
175 | }
176 |
177 | /**
178 | * 去除字符串的首尾回车和空格
179 | *
180 | * @param str
181 | * @return
182 | */
183 | private static String handleCell(String str) {
184 | str = str.replaceAll("^\n|\n$", "");//去除首尾换行符
185 | str = str.trim();//去除首尾空格
186 | return str;
187 | }
188 |
189 | }
190 |
--------------------------------------------------------------------------------
/app/src/main/java/com/potato/timetable/ui/coursedetails/CourseDetailsActivity.java:
--------------------------------------------------------------------------------
1 | package com.potato.timetable.ui.coursedetails;
2 |
3 | import android.content.DialogInterface;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.view.Menu;
7 | import android.view.MenuItem;
8 | import android.view.View;
9 | import android.widget.Button;
10 | import android.widget.ImageView;
11 | import android.widget.TextView;
12 | import android.widget.Toast;
13 |
14 | import androidx.annotation.NonNull;
15 | import androidx.annotation.Nullable;
16 | import androidx.appcompat.app.ActionBar;
17 | import androidx.appcompat.app.AlertDialog;
18 | import androidx.appcompat.app.AppCompatActivity;
19 | import androidx.cardview.widget.CardView;
20 |
21 | import com.potato.timetable.R;
22 | import com.potato.timetable.bean.Course;
23 | import com.potato.timetable.ui.editcourse.EditActivity;
24 | import com.potato.timetable.ui.main.MainActivity;
25 | import com.potato.timetable.util.FileUtils;
26 | import com.potato.timetable.util.Utils;
27 |
28 | import java.util.List;
29 |
30 | public class CourseDetailsActivity extends AppCompatActivity {
31 | public static final String KEY_COURSE_INDEX = "course_index";
32 | private static final String[] aStrWeek = new String[]{
33 | "周一", "周二", "周三", "周四", "周五", "周六", "周日"
34 | };
35 | public static final int EDIT_ID = 0;
36 | private int mIndex;
37 |
38 | @Override
39 | protected void onCreate(Bundle savedInstanceState) {
40 | super.onCreate(savedInstanceState);
41 | setContentView(R.layout.activity_course_details);
42 | Button button = findViewById(R.id.btn_edit);
43 |
44 | setActionBar();
45 |
46 | mIndex = getIntent().getIntExtra(KEY_COURSE_INDEX, 0);
47 |
48 | setCourseTextView();
49 |
50 | button.setOnClickListener(new View.OnClickListener() {
51 | @Override
52 | public void onClick(View view) {
53 | Intent intent = new Intent(CourseDetailsActivity.this, EditActivity.class);
54 | intent.putExtra(EditActivity.EXTRA_COURSE_INDEX, mIndex);
55 | startActivityForResult(intent, EDIT_ID);
56 | }
57 | });
58 |
59 | ImageView imageView = findViewById(R.id.iv_bg);
60 | setCardViewAlpha();
61 |
62 |
63 | Utils.setBackGround(this, imageView);
64 |
65 | }
66 |
67 | /**
68 | * 设置CardView透明度
69 | */
70 | private void setCardViewAlpha() {
71 | CardView cardView = findViewById(R.id.cv_course_details);
72 | Utils.setCardViewAlpha(cardView);
73 | }
74 |
75 | /**
76 | * 通知主界面更新
77 | */
78 | private void setUpdateResult() {
79 | Intent intent = new Intent();
80 | intent.putExtra(EditActivity.EXTRA_UPDATE_TIMETABLE, true);
81 | setResult(RESULT_OK, intent);
82 | }
83 |
84 | @Override
85 | public boolean onCreateOptionsMenu(Menu menu) {
86 | getMenuInflater().inflate(R.menu.menu_course_details, menu);
87 | return super.onCreateOptionsMenu(menu);
88 | }
89 |
90 |
91 | @Override
92 | protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
93 | super.onActivityResult(requestCode, resultCode, data);
94 | if (requestCode == EDIT_ID) {
95 | if (data != null) {
96 | if (data.getBooleanExtra(EditActivity.EXTRA_UPDATE_TIMETABLE, false)) {
97 | setCourseTextView();
98 | }
99 | setResult(RESULT_OK, data);//回传给MainActivity,让其更新课表
100 | }
101 |
102 | }
103 | }
104 |
105 | /**
106 | * 初始化TextView
107 | */
108 | private void setCourseTextView() {
109 | Course course = MainActivity.sCourseList.get(mIndex);
110 | TextView textView = findViewById(R.id.tv_class_name);
111 | textView.setText(course.getName());
112 |
113 | textView = findViewById(R.id.tv_class_room);
114 |
115 | textView.setText(course.getClassRoom());
116 |
117 | textView = findViewById(R.id.tv_class_num);
118 |
119 | int class_start = course.getClassStart();
120 | int class_num = course.getClassLength();
121 | textView.setText(String.format(getString(R.string.schedule_section),
122 | aStrWeek[course.getDayOfWeek() - 1], class_start, (class_start + class_num - 1)));
123 |
124 | textView = findViewById(R.id.tv_week_of_term);
125 | // textView.setText(String.format(getString(R.string.week_of_term_format),
126 | // course.getWeekOfTerm(), course.getWeekOptions()));
127 | textView.setText(Utils.getFormatStringFromWeekOfTerm(course.getWeekOfTerm()));
128 |
129 | textView = findViewById(R.id.tv_teacher);
130 | textView.setText(course.getTeacher());
131 | }
132 |
133 | /**
134 | * 初始化ActionBar
135 | */
136 | private void setActionBar() {
137 | ActionBar actionBar = getSupportActionBar();
138 | actionBar.setDisplayHomeAsUpEnabled(true);
139 | actionBar.setTitle(R.string.course_details);
140 | }
141 |
142 | /**
143 | * 菜单栏
144 | *
145 | * @param item
146 | * @return
147 | */
148 |
149 | @Override
150 | public boolean onOptionsItemSelected(@NonNull MenuItem item) {
151 | switch (item.getItemId()) {
152 | case android.R.id.home:
153 | finish();
154 | break;
155 | case R.id.menu_delete:
156 | final AlertDialog alertDialog = new AlertDialog.Builder(this)
157 | .setTitle("提示")
158 | .setMessage("您确定要删除该课程吗?")
159 | .create();
160 | alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
161 | @Override
162 | public void onClick(DialogInterface dialogInterface, int i) {
163 | MainActivity.sCourseList.remove(mIndex);
164 | new FileUtils>().saveToJson(CourseDetailsActivity.this,
165 | MainActivity.sCourseList,
166 | FileUtils.TIMETABLE_FILE_NAME);
167 | Toast.makeText(CourseDetailsActivity.this, "成功删除", Toast.LENGTH_SHORT).show();
168 | setUpdateResult();
169 | alertDialog.dismiss();
170 | finish();
171 |
172 | }
173 | });
174 | alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
175 | @Override
176 | public void onClick(DialogInterface dialogInterface, int i) {
177 | alertDialog.dismiss();
178 | }
179 | });
180 | alertDialog.show();
181 | default:
182 | break;
183 | }
184 | return super.onOptionsItemSelected(item);
185 | }
186 | }
187 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_course_details.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
15 |
16 |
21 |
22 |
31 |
32 |
33 |
38 |
39 |
43 |
44 |
52 |
53 |
62 |
63 |
64 |
65 |
66 |
74 |
78 |
79 |
85 |
86 |
93 |
94 |
95 |
99 |
100 |
106 |
107 |
114 |
115 |
116 |
120 |
121 |
127 |
128 |
135 |
136 |
137 |
138 |
139 |
143 |
144 |
150 |
151 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
--------------------------------------------------------------------------------
/app/src/main/java/com/potato/timetable/util/OkHttpUtils.java:
--------------------------------------------------------------------------------
1 | package com.potato.timetable.util;
2 |
3 | import com.franmontiel.persistentcookiejar.PersistentCookieJar;
4 | import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
5 | import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
6 | import com.potato.timetable.MyApplication;
7 |
8 | import java.io.BufferedInputStream;
9 | import java.io.BufferedOutputStream;
10 | import java.io.File;
11 | import java.io.FileOutputStream;
12 | import java.io.IOException;
13 | import java.io.UnsupportedEncodingException;
14 | import java.util.Objects;
15 | import java.util.concurrent.TimeUnit;
16 |
17 | import okhttp3.CookieJar;
18 | import okhttp3.MediaType;
19 | import okhttp3.OkHttpClient;
20 | import okhttp3.Request;
21 | import okhttp3.Response;
22 |
23 | public class OkHttpUtils {
24 | private static final byte[] EMPTY_BYTES = new byte[0];
25 | public static final MediaType JSON
26 | = MediaType.get("application/json; charset=utf-8");
27 |
28 | /**
29 | * 自动存储保存cookies
30 | *
31 | * private static class MyCookieJar implements CookieJar {
32 | * private Map> cookieStore = new HashMap<>();
33 | *
34 | * @NotNull
35 | * @Override public List loadForRequest(@NotNull HttpUrl httpUrl) {
36 | * List cookies = cookieStore.get(httpUrl.host());
37 | * return cookies != null ? cookies : new ArrayList();
38 | * }
39 | * @Override public void saveFromResponse(@NotNull HttpUrl httpUrl, @NotNull List list) {
40 | * cookieStore.put(httpUrl.host(), list);
41 | * }
42 | * }
43 | *
44 | * private static MyCookieJar cookieJar = new MyCookieJar();
45 | */
46 |
47 | /**
48 | * 静态内部类单例模式
49 | * 需要时加载
50 | *
51 | * 只有第一次调用getInstance方法时,
52 | * 虚拟机才加载 Inner 并初始化okHttpClient,
53 | * 只有一个线程可以获得对象的初始化锁,其他线程无法进行初始化,
54 | * 保证对象的唯一性。目前此方式是所有单例模式中最推荐的模式。
55 | */
56 | private static class Inner {
57 | private static final CookieJar cookieJar = new PersistentCookieJar(
58 | new SetCookieCache(),
59 | new SharedPrefsCookiePersistor(MyApplication.getApplication()));
60 | private static OkHttpClient okHttpClient = new OkHttpClient.Builder()
61 | .cookieJar(cookieJar)
62 | .connectTimeout(5, TimeUnit.SECONDS)
63 | .readTimeout(5, TimeUnit.SECONDS)
64 | .writeTimeout(5, TimeUnit.SECONDS)
65 | .followRedirects(true)
66 | .build();
67 | }
68 |
69 | public static OkHttpClient getOkHttpClient() {
70 | return Inner.okHttpClient;
71 | }
72 |
73 | public static void setFollowRedirects(boolean followRedirects) {
74 | if (Inner.okHttpClient.followRedirects() != followRedirects) {
75 | Inner.okHttpClient = Inner.okHttpClient.newBuilder()
76 | .followRedirects(followRedirects)
77 | .build();
78 | }
79 | }
80 |
81 | public static CookieJar getCookieJar() {
82 | return Inner.cookieJar;
83 | }
84 |
85 | /**
86 | * 下载文件到本地
87 | *
88 | * @param url
89 | * @param path 文件夹地址
90 | * @param name 文件名
91 | * @return
92 | */
93 | public static boolean downloadToLocal(String url, String path, String name) {
94 | return downloadToLocal(createRequest(url), path, name);
95 | }
96 |
97 | /**
98 | * 下载文件到本地
99 | *
100 | * @param path 文件夹地址
101 | * @param name 文件名
102 | * @return
103 | */
104 | public static boolean downloadToLocal(Request request, String path, String name) {
105 | BufferedInputStream bis = null;
106 | BufferedOutputStream bos = null;
107 | try (Response response = getOkHttpClient().newCall(request).execute()) {
108 | if (response.code() == 200) {
109 | File file = new File(path);
110 | if (!file.exists()) {
111 | if (!file.mkdirs()) {
112 | return false;
113 | }
114 | } else {
115 | if (!file.isDirectory()) {
116 | return false;
117 | }
118 | }
119 |
120 | bos = new BufferedOutputStream(
121 | new FileOutputStream(path + File.separator + name));
122 | bis = new BufferedInputStream(response.body().byteStream());
123 |
124 | byte[] buffer = new byte[1024];
125 | int len;
126 | while ((len = bis.read(buffer, 0, 1024)) != -1) {
127 | bos.write(buffer, 0, len);
128 | }
129 | bos.flush();
130 |
131 | return true;
132 | }
133 | } catch (IOException | NullPointerException e) {
134 | e.printStackTrace();
135 | } finally {
136 | try {
137 | if (bis != null) {
138 | bis.close();
139 | }
140 | if (bos != null) {
141 | bos.close();
142 | }
143 | } catch (IOException e) {
144 | e.printStackTrace();
145 | }
146 |
147 | }
148 | return false;
149 | }
150 |
151 | /**
152 | * 下载文本内容
153 | *
154 | * @param url
155 | * @return
156 | */
157 | public static String downloadText(String url) {
158 | return downloadText(createRequest(url));
159 | }
160 |
161 | /**
162 | * 下载文本内容
163 | *
164 | * @param request
165 | * @return
166 | */
167 | public static String downloadText(Request request) {
168 | return downloadText(request, "UTF-8");
169 | }
170 |
171 | /**
172 | * 下载文本内容
173 | *
174 | * @param url
175 | * @return
176 | */
177 | public static String downloadText(String url, String encoding) {
178 | return downloadText(createRequest(url), encoding);
179 | }
180 |
181 | /**
182 | * 下载文本内容
183 | *
184 | * @param request
185 | * @param encoding
186 | * @return
187 | */
188 | public static String downloadText(Request request, String encoding) {
189 | try {
190 | return new String(downloadRaw(request), encoding);
191 | } catch (UnsupportedEncodingException e) {
192 | e.printStackTrace();
193 | return "";
194 | }
195 | }
196 |
197 | /**
198 | * 下载字节码
199 | *
200 | * @param url
201 | * @return
202 | */
203 | public static byte[] downloadRaw(String url) {
204 | Request request = new Request.Builder()
205 | .url(url)
206 | .build();
207 | return downloadRaw(request);
208 | }
209 |
210 | /**
211 | * 下载字节码
212 | *
213 | * @param request
214 | * @return 返回下载内容
215 | */
216 | public static byte[] downloadRaw(Request request) {
217 | try (Response response = getOkHttpClient().newCall(request).execute()) {
218 | if (response.code() == 200) {
219 | if (response.body() != null) {
220 | return Objects.requireNonNull(response.body()).bytes();
221 | }
222 | }
223 | } catch (IOException | NullPointerException e) {
224 | e.printStackTrace();
225 | }
226 | return EMPTY_BYTES;
227 | }
228 |
229 | public static Request createRequest(String url) {
230 | return new Request.Builder()
231 | .url(url)
232 | .build();
233 | }
234 | }
235 |
--------------------------------------------------------------------------------
/app/src/main/java/com/potato/timetable/ui/editcourse/WeekOfTermSelectDialog.java:
--------------------------------------------------------------------------------
1 | package com.potato.timetable.ui.editcourse;
2 |
3 | import android.app.Dialog;
4 | import android.content.Context;
5 | import android.os.Bundle;
6 | import android.view.Gravity;
7 | import android.view.KeyboardShortcutGroup;
8 | import android.view.LayoutInflater;
9 | import android.view.Menu;
10 | import android.view.View;
11 | import android.view.ViewGroup;
12 | import android.view.Window;
13 | import android.view.WindowManager;
14 | import android.widget.Button;
15 | import android.widget.CheckBox;
16 | import android.widget.CompoundButton;
17 |
18 | import androidx.annotation.NonNull;
19 | import androidx.annotation.Nullable;
20 | import androidx.recyclerview.widget.GridLayoutManager;
21 | import androidx.recyclerview.widget.RecyclerView;
22 |
23 | import com.potato.timetable.R;
24 | import com.potato.timetable.util.Config;
25 |
26 | import java.util.ArrayList;
27 | import java.util.List;
28 |
29 |
30 | public class WeekOfTermSelectDialog extends Dialog {
31 | private final List list = new ArrayList<>(Config.getMaxWeekNum());
32 | private final int mWeekOfTerm;
33 | private final Context mContext;
34 | private DialogAdapter dialogAdapter;
35 | private CheckBox selectAll;
36 | private CheckBox singleWeek;
37 | private CheckBox doubleWeek;
38 | private View.OnClickListener positiveListener;
39 | private View.OnClickListener nativeListener;
40 |
41 |
42 | public WeekOfTermSelectDialog(@NonNull Context context, int weekOfTerm) {
43 | super(context, R.style.CustomDialog);
44 | this.mWeekOfTerm = weekOfTerm;
45 | this.mContext = context;
46 | }
47 |
48 | /**
49 | * 设置dialog居下占满屏幕
50 | */
51 | private void changeDialogStyle() {
52 | Window window = getWindow();
53 | if (window != null) {
54 | WindowManager.LayoutParams attr = window.getAttributes();
55 | if (attr != null) {
56 | attr.height = ViewGroup.LayoutParams.WRAP_CONTENT;
57 | attr.width = ViewGroup.LayoutParams.MATCH_PARENT;
58 | attr.gravity = Gravity.BOTTOM;
59 | window.setAttributes(attr);
60 | }
61 | }
62 | }
63 |
64 | public int getWeekOfTerm() {
65 | int weekOfTerm = 0;
66 | for (int i = 0, len = list.size(); i < len; i++) {
67 | if (list.get(i)) {
68 | //Log.d("weekofterm",String.valueOf(i));
69 | weekOfTerm++;
70 | }
71 | if (i != len - 1) {//最后不移动
72 | weekOfTerm = weekOfTerm << 1;
73 | }
74 | }
75 | return weekOfTerm;
76 | }
77 |
78 |
79 | @Override
80 | protected void onCreate(Bundle savedInstanceState) {
81 | super.onCreate(savedInstanceState);
82 | setContentView(R.layout.dialog_select_week_of_term);
83 | init();
84 | RecyclerView recyclerView = findViewById(R.id.rv_week_of_term);
85 | GridLayoutManager gridLayoutManager =
86 | new GridLayoutManager(mContext, 5);
87 | recyclerView.setLayoutManager(gridLayoutManager);
88 |
89 | dialogAdapter = new DialogAdapter(list);
90 | recyclerView.setAdapter(dialogAdapter);
91 |
92 | selectAll = findViewById(R.id.check_box_select_all);
93 | selectAll.setOnClickListener(view -> {
94 | boolean b = selectAll.isChecked();
95 | if (b) {
96 | singleWeek.setChecked(false);
97 | doubleWeek.setChecked(false);
98 | }
99 | for (int i = 0, len = list.size(); i < len; i++) {
100 | dialogAdapter.checkBoxList.get(i).setChecked(b);
101 | }
102 | });
103 |
104 | singleWeek = findViewById(R.id.check_box_single_week);
105 | singleWeek.setOnClickListener(view -> {
106 | if (singleWeek.isChecked()) {
107 | selectAll.setChecked(false);
108 | doubleWeek.setChecked(false);
109 | for (int i = 0, len = list.size(); i < len; i++) {
110 | dialogAdapter.checkBoxList.get(i).setChecked((i + 1) % 2 == 1);
111 | }
112 | }
113 |
114 | });
115 |
116 | doubleWeek = findViewById(R.id.check_box_double_week);
117 | doubleWeek.setOnClickListener(view -> {
118 | if (doubleWeek.isChecked()) {
119 | singleWeek.setChecked(false);
120 | selectAll.setChecked(false);
121 | for (int i = 0, len = list.size(); i < len; i++) {
122 | dialogAdapter.checkBoxList.get(i).setChecked((i + 1) % 2 == 0);
123 | }
124 | }
125 | });
126 | Button cancelBtn = findViewById(R.id.btn_cancel);
127 | Button yesBtn = findViewById(R.id.btn_yes);
128 | cancelBtn.setOnClickListener(nativeListener);
129 | yesBtn.setOnClickListener(positiveListener);
130 |
131 | changeDialogStyle();
132 | }
133 |
134 | public void setPositiveBtn(View.OnClickListener listener) {
135 | positiveListener = listener;
136 | }
137 |
138 | public void setNativeBtn(View.OnClickListener listener) {
139 | nativeListener = listener;
140 | }
141 |
142 | private void init() {
143 | if (mWeekOfTerm == -1) {
144 | for (int i = 0, len = Config.getMaxWeekNum(); i < len; i++) {
145 | list.add(false);
146 | }
147 | } else {
148 | for (int i = Config.getMaxWeekNum() - 1; i >= 0; i--) {
149 | list.add(((mWeekOfTerm >> i) & 0x01) == 1);
150 | }
151 | }
152 |
153 | }
154 |
155 | @Override
156 | public void onProvideKeyboardShortcuts(List data, @Nullable Menu menu, int deviceId) {
157 |
158 | }
159 |
160 | @Override
161 | public void onPointerCaptureChanged(boolean hasCapture) {
162 |
163 | }
164 |
165 | private static class DialogAdapter extends RecyclerView.Adapter {
166 | private final List checkBoxList = new ArrayList<>();
167 | private final List resultList;
168 |
169 | public DialogAdapter(List resultList) {
170 | this.resultList = resultList;
171 | }
172 |
173 | @NonNull
174 | @Override
175 | public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
176 | View view = LayoutInflater.from(parent.getContext())
177 | .inflate(R.layout.week_of_term_checkbox, parent, false);
178 | return new ViewHolder(view);
179 | }
180 |
181 | @Override
182 | public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
183 | checkBoxList.add(holder.checkBox);
184 | holder.checkBox.setChecked(resultList.get(position));
185 | holder.checkBox.setText(String.valueOf(position + 1));
186 | holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
187 | @Override
188 | public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
189 | // Log.d("checkbox", "发生改变");
190 | resultList.set(position, b);
191 | }
192 | });
193 | }
194 |
195 | @Override
196 | public int getItemCount() {
197 | return resultList.size();
198 | }
199 |
200 | private static class ViewHolder extends RecyclerView.ViewHolder {
201 | public final CheckBox checkBox;
202 |
203 | public ViewHolder(View view) {
204 | super(view);
205 | checkBox = view.findViewById(R.id.checkBox);
206 | }
207 |
208 | }
209 | }
210 | }
211 |
--------------------------------------------------------------------------------
/app/src/main/java/com/potato/timetable/ui/settime/SetTimeActivity.java:
--------------------------------------------------------------------------------
1 | package com.potato.timetable.ui.settime;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.view.LayoutInflater;
6 | import android.view.Menu;
7 | import android.view.MenuItem;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.ImageView;
11 | import android.widget.LinearLayout;
12 | import android.widget.TextView;
13 |
14 | import androidx.annotation.NonNull;
15 | import androidx.appcompat.app.ActionBar;
16 | import androidx.appcompat.app.AlertDialog;
17 | import androidx.appcompat.app.AppCompatActivity;
18 | import androidx.recyclerview.widget.LinearLayoutManager;
19 | import androidx.recyclerview.widget.RecyclerView;
20 |
21 | import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
22 | import com.bigkoo.pickerview.view.OptionsPickerView;
23 | import com.potato.timetable.R;
24 | import com.potato.timetable.bean.Time;
25 | import com.potato.timetable.ui.main.MainActivity;
26 | import com.potato.timetable.util.Config;
27 | import com.potato.timetable.util.FileUtils;
28 | import com.potato.timetable.util.Utils;
29 |
30 | import java.util.ArrayList;
31 | import java.util.List;
32 |
33 | public class SetTimeActivity extends AppCompatActivity {
34 | private OptionsPickerView mOptionsPv;
35 | private final TimeAdapter timeAdapter = new TimeAdapter();
36 | private final List timeList = new ArrayList<>(18 * 12);
37 | private final Time[] times = new Time[Config.getMaxClassNum()];
38 | public static final String EXTRA_UPDATE_Time = "time";
39 |
40 | @Override
41 | protected void onCreate(Bundle savedInstanceState) {
42 | super.onCreate(savedInstanceState);
43 | setContentView(R.layout.activity_set_time);
44 | setActionBar();
45 | RecyclerView recyclerView = findViewById(R.id.rv_time_list);
46 | LinearLayoutManager layoutManager =
47 | new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
48 | recyclerView.setLayoutManager(layoutManager);
49 |
50 | recyclerView.setAdapter(timeAdapter);
51 |
52 | initData();
53 | ImageView imageView = findViewById(R.id.iv_bg_set_time);
54 | Utils.setBackGround(this, imageView);
55 |
56 | }
57 |
58 | /**
59 | * 初始化数据
60 | */
61 | private void initData() {
62 | for (int i = 0, len = times.length; i < len; i++) {
63 | try {
64 | if (MainActivity.sTimes == null || MainActivity.sTimes.length == 0) {
65 | times[i] = new Time();
66 | } else {
67 | times[i] = (Time) MainActivity.sTimes[i].clone();
68 | }
69 |
70 | } catch (CloneNotSupportedException e) {
71 | e.printStackTrace();
72 | }
73 |
74 | }
75 | for (int i = 6; i < 24; i++) {
76 | for (int j = 0; j < 12; j++) {
77 | timeList.add(Utils.formatTime(i) + ":" + Utils.formatTime(j * 5));
78 | }
79 | }
80 | }
81 |
82 | /**
83 | * 初始化ActionBar
84 | */
85 | private void setActionBar() {
86 | ActionBar actionBar = getSupportActionBar();
87 | actionBar.setDisplayHomeAsUpEnabled(true);
88 | actionBar.setTitle(R.string.set_time);
89 | }
90 |
91 | /**
92 | * 通知主界面更新
93 | */
94 | private void setUpdateResult() {
95 | // Log.d("settime", "通知更新");
96 | Intent intent = new Intent();
97 | intent.putExtra(EXTRA_UPDATE_Time, true);
98 | setResult(RESULT_OK, intent);
99 | }
100 |
101 | @Override
102 | public boolean onCreateOptionsMenu(Menu menu) {
103 | getMenuInflater().inflate(R.menu.menu_set_time, menu);
104 | return super.onCreateOptionsMenu(menu);
105 | }
106 |
107 | @Override
108 | public boolean onOptionsItemSelected(@NonNull MenuItem item) {
109 | int id = item.getItemId();
110 | if (id == android.R.id.home) {
111 | finish();
112 | } else if (id == R.id.menu_save) {
113 | AlertDialog alertDialog = new AlertDialog.Builder(this)
114 | .setTitle("提示")
115 | .setMessage("是否保存该时间表并退出?")
116 | .setPositiveButton("确定", (dialogInterface, i) -> {
117 | new FileUtils