LayoutXML / Twelveish

🕛 Twelveish - Android Wear/Wear OS Watch Face
https://play.google.com/store/apps/details?id=com.layoutxml.twelveish
GNU General Public License v3.0
43 stars 19 forks source link

Functional phone app #23

Open augustuen opened 3 years ago

augustuen commented 3 years ago

This pull request represents a functional and presentable phone/companion/handheld app. There is still a lot of improvements to be made to the look and function of the app, but this version is functional and pretty enough.

Features

Some preferences have been temporarily removed from the phone app to fit with working features:

Communicator implementation

Loading preferences from watch to phone

Communicator.requestPreferences(Context context, WeakReference<WatchPreviewView> listenerActivity)

The phone initiates a transfer of preferences by sending a DataRequest with the boolean "rokas-twelveish-dr" set to true. The watch app then loops through all preferences, adding each key and value to a string array, with each value directly following its key. The array is then returned to the phone in a DataRequest with "rokas-twelveish-dr2"=true.

Upon reception of the array, the phone app loops through all entries and checks for a matching key in each SettingsManager hashmap, adding the value to the corresponding map. The entire SettingsManager is then converted to JSON and saved to a the file test.json. This is partially done because of a difficulty with sharing a SettingsManager between HomeScreen and CustomizationScreen.

Saving (all) preferences to watch from phone

Communicator.sendWatchFace(SettingsManager settingsManager, Context context)

Phone loops through each SettingsManager hashmap, adding every key/value pair to a string array, along with the value type. Every preference is added to the array in this format:

  1. String key
  2. String value
  3. String type (String, Integer, or Boolean)

The string array is sent in a DataRequest with boolean rokas-twelveish-dr2 set to true. The watch loops through the string array, converting the second string in each triplet to the appropriate variable type before adding it to preferences.

Pinging

Communicator.ping()

After a connection to the watch has been established through Communicator.initiateHandshake(), a postDelayed Handler is created, running every 5000 milliseconds. This handler calls ping(), sending a DataRequest with rokas-twelveish-fire = true. This request is answered with rokas-twelveish-ice=true and a timestamp (the current system time in milliseconds on the watch) stored in rokas-twelveish-timestamp. This timestamp is stored as lastPing, which is then compared to the current system time of the phone.

If the difference between the current time and the last received timestamp is greater than 15000 milliseconds (15 seconds), the watch is considered disconnected, isWatchConnected is set to false, and handshake initiation is restarted.

Loading preferences

WatchPreviewView.WatchPreviewView(Context context, @Nullable AttributeSet attrs)

When created from the HomeScreen activity, WatchPreviewView tells the communicator to request all preferences from the watch, as described in Communicator.requestPreferences. If the request was succesful, the preferences are saved to the local JSON file as a cache. This file is then loaded by the CustomizationScreen activity. If no such file exists, a new SettingsManageris initiated and saved to the file.

This request includes the changes found in PR #22.