-
Notifications
You must be signed in to change notification settings - Fork 0
/
Syntaxx.java
127 lines (110 loc) · 4.29 KB
/
Syntaxx.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.codenlight.githubfileretrieve;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.codenlight.githubfileretrieve.utils.FileObject;
import com.codenlight.githubfileretrieve.utils.ThemeChangerDialog;
import com.pddstudio.highlightjs.HighlightJsView;
import com.pddstudio.highlightjs.models.Language;
import com.pddstudio.highlightjs.models.Theme;
import java.util.Random;
public class SyntaxActivity extends AppCompatActivity implements
SwipeRefreshLayout.OnRefreshListener,
HighlightJsView.OnThemeChangedListener,
ThemeChangerDialog.ThemeChangeListener {
private SwipeRefreshLayout swipeRefreshLayout;
private HighlightJsView highlightJsView;
private FileObject fileObject;
private ThemeChangerDialog themeChangerDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_syntax);
fileObject = (FileObject) getIntent().getExtras().getSerializable("fileObject");
if(getActionBar() != null) {
getActionBar().setTitle(fileObject.getAbsoluteFilePath());
getActionBar().setDisplayHomeAsUpEnabled(true);
}
if(getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(fileObject.getAbsoluteFilePath());
}
//set and assign swipe refresh listener
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
swipeRefreshLayout.setOnRefreshListener(this);
//find and instantiate the view
highlightJsView = (HighlightJsView) findViewById(R.id.highlight_view);
//register theme change listener
highlightJsView.setOnThemeChangedListener(this);
//change theme and set language to auto detect
highlightJsView.setTheme(Theme.SCHOOL_BOOK);
highlightJsView.setHighlightLanguage(Language.AUTO_DETECT);
highlightJsView.setShowLineNumbers(true);
//load the source
highlightJsView.setSource(fileObject.getUrl());
}
@Override
protected void onResume() {
super.onResume();
themeChangerDialog = new ThemeChangerDialog(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.menu_theme_switch, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
case R.id.menu_switch_theme:
themeChangerDialog.show(this);
break;
case R.id.menu_check_line_numbers:
item.setChecked(!item.isChecked());
onShowLineNumbersToggled(item.isChecked());
break;
case R.id.menu_check_zoom:
item.setChecked(!item.isChecked());
onZoomSupportToggled(item.isChecked());
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
private void onShowLineNumbersToggled(boolean enableLineNumbers) {
highlightJsView.setShowLineNumbers(enableLineNumbers);
highlightJsView.refresh();
}
private void onZoomSupportToggled(boolean enableZooming) {
highlightJsView.setZoomSupportEnabled(enableZooming);
highlightJsView.refresh();
}
@Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(true);
highlightJsView.setTheme(getRandom(Theme.values()));
highlightJsView.refresh();
}
private <T> T getRandom(T... items) {
return items[new Random().nextInt(items.length)];
}
@Override
public void onThemeChanged(@NonNull Theme theme) {
swipeRefreshLayout.setRefreshing(false);
Toast.makeText(this, "Theme: " + theme.name(), Toast.LENGTH_SHORT).show();
}
@Override
public void onChangeTheme(@NonNull Theme theme) {
highlightJsView.setTheme(theme);
highlightJsView.refresh();
}
}