-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathMainActivity.kt
More file actions
168 lines (142 loc) · 6.05 KB
/
MainActivity.kt
File metadata and controls
168 lines (142 loc) · 6.05 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package net.activitywatch.android
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.navigation.NavigationView
import androidx.core.view.GravityCompat
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import androidx.fragment.app.Fragment
import android.util.Log
import net.activitywatch.android.databinding.ActivityMainBinding
import net.activitywatch.android.fragments.TestFragment
import net.activitywatch.android.fragments.WebUIFragment
import net.activitywatch.android.watcher.UsageStatsWatcher
private const val TAG = "MainActivity"
const val baseURL = "http://127.0.0.1:5600"
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener, WebUIFragment.OnFragmentInteractionListener {
private lateinit var binding: ActivityMainBinding
val version: String
get() {
return packageManager.getPackageInfo(packageName, 0).versionName
}
override fun onFragmentInteraction(item: Uri) {
Log.w(TAG, "URI onInteraction listener not implemented")
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// If first time, or usage not allowed, show onboarding activity
val prefs = AWPreferences(this)
if (prefs.isFirstTime() || !UsageStatsWatcher.isUsageAllowed(this)) {
Log.i(TAG, "First time or usage not allowed, starting onboarding activity")
val intent = Intent(this, OnboardingActivity::class.java)
startActivity(intent)
}
// Set up UI
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
// Set up alarm to send heartbeats
val usw = UsageStatsWatcher(this)
usw.setupAlarm()
binding.navView.setNavigationItemSelectedListener(this)
val ri = RustInterface(this)
ri.startServerTask(this)
if (savedInstanceState != null) {
return
}
val firstFragment = WebUIFragment.newInstance(baseURL)
supportFragmentManager.beginTransaction()
.add(R.id.fragment_container, firstFragment).commit()
}
override fun onResume() {
super.onResume()
// Ensures data is always fresh when app is opened,
// even if it was up to an hour since the last logging-alarm was triggered.
val usw = UsageStatsWatcher(this)
usw.sendHeartbeats()
}
override fun onBackPressed() {
if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
binding.drawerLayout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
// Checks the orientation of the screen
if (newConfig.orientation === Configuration.ORIENTATION_LANDSCAPE) {
Log.i(TAG, "Screen orientation changed to landscape")
} else if (newConfig.orientation === Configuration.ORIENTATION_PORTRAIT){
Log.i(TAG, "Screen orientation changed to portrait")
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> {
Snackbar.make(binding.coordinatorLayout, "The settings button was clicked, but it's not yet implemented!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
true
}
else -> super.onOptionsItemSelected(item)
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
var fragmentClass: Class<out Fragment>? = null
var url: String? = null
// Handle navigation view item clicks here.
when (item.itemId) {
R.id.nav_dashboard -> {
fragmentClass = TestFragment::class.java
}
R.id.nav_activity -> {
fragmentClass = WebUIFragment::class.java
url = "$baseURL/#/activity/unknown/"
}
R.id.nav_buckets -> {
fragmentClass = WebUIFragment::class.java
url = "$baseURL/#/buckets/"
}
R.id.nav_settings -> {
fragmentClass = WebUIFragment::class.java
url = "$baseURL/#/settings/"
}
R.id.nav_share -> {
Snackbar.make(binding.coordinatorLayout, "The share button was clicked, but it's not yet implemented!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
R.id.nav_send -> {
Snackbar.make(binding.coordinatorLayout, "The send button was clicked, but it's not yet implemented!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
}
val fragment: Fragment? = try {
if (fragmentClass === WebUIFragment::class.java && url != null) {
WebUIFragment.newInstance(url)
} else {
fragmentClass?.newInstance()
}
} catch (e: Exception) {
e.printStackTrace()
null
}
if(fragment != null) {
// Insert the fragment by replacing any existing fragment
val fragmentManager = supportFragmentManager
fragmentManager.beginTransaction().replace(R.id.fragment_container, fragment).commit()
}
binding.drawerLayout.closeDrawer(GravityCompat.START)
return true
}
}