Can't insert card in timeline - google-glass

I'm not able to insert a card into the user timeline using the GDK. My code is very simple :
TimelineManager timelineManager = TimelineManager.from(this);
Card card = new Card(this);
card.setText("Text").setInfo("Info").addImage(R.drawable.ic_launcher);
timelineManager.insert(card);
The issue returned by the insert method is the following :
Caused by: java.lang.IllegalArgumentException: Unknown URL content://com.google.android.glass.timeline/past_timeline_table
Is anyone also having this issue?
Julien

This feature was added in XE12 / release 2 of the GDK.
Note that setInfo() is now setFootnote().

Where are you trying to insert the card from? If you are still having trouble you can checkout my Hello Glass repo; I was able to create and display cards without issue: https://github.com/DasCody/Hello-Glass
Here is an example:
package com.codyengel.helloglass;
import com.google.android.glass.app.Card;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
public class Magic extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* We're creating a card for the interface.
*
* More info here: http://developer.android.com/guide/topics/ui/themes.html
*/
Card card1 = new Card(this);
card1.setText("Hello, Sir!");
card1.setInfo("..or Ma'am");
View card1View = card1.toView();
// Display the card we just created
setContentView(card1View);
}
}

The best you can do at the moment ist look at the GDK Sample Projects.
(File -> New -> Other -> Android Sample Project -> Choose GDK Sneak Peak as Build Target)
Have a close look at the Compass, Stopwatch and Timer examples and let them run on your glass.
Here is a code snippet (just the relevant code) how they create a new Card in the timeline in the Timer example:
TimelineManager mTimelineManager;
LiveCard mLiveCard;
TimerDrawer mTimerDrawer;
mLiveCard = mTimelineManager.getLiveCard(LIVE_CARD_ID);
mLiveCard.enableDirectRendering(true).getSurfaceHolder().addCallback(mTimerDrawer);
mLiveCard.setNonSilent(true);
Intent menuIntent = new Intent(this, MenuActivity.class);
mLiveCard.setAction(PendingIntent.getActivity(this, 0, menuIntent, 0));
mLiveCard.publish();
Short explanation:
a LiveCard is a Card you draw on. (potentially quite frequent)
TimerDrawer is a custom class that does the drawing.
getLiveCard creates a new Card in the timeline with the given string ID.
MenuActivity is a custom activity that is issued when you tap the LiveCard. (has to always be defined)
So basically this code creates a new Card, defines how to draw it, defines what happens when it is tapped and publishes it. There's much more code involved, look at the sample. Hope this guides you in the right direction.

Related

Dynamic theme color at run time jetpack compose

I'm new to Jetpack Compose, so I'm struggling to implement a feature which is dynamic colors (and font, size,... but I think they are the same so I'll just focus on color) at run time from backend. I'll let the app the some default colors, and a whole default splash screen just to load the colors setting from the backend. In case the API request failed, it would use the last succeeded requested colors or just the default color.
Tutorials I found round the internet was just about changing the dark/light theme at run time, not changing a specific color in the color pack. In those tutorials, the color is defined in Colors.kt file which is not a composable or class or object, ...
I imagine the color within lightColors or darkColors would be something like this.
return lightColors(
primary = Color(android.graphics.Color.parseColor("#" + dynamicColorMap["One"])),
...
}
And when dynamicColorMap changes in the splashscreen, all screen later will have reference to the new value, but I don't know how to update its variable outside of a composable.
I thought of using DB to store the colors, but getting the data from DB is async, so it cannot be query in the default Colors.kt like var colorOne = DBManager.getColor("One"), I can run the async task in my splash screen before changing to the next screen but then the problem again is how to have a global state that my theme composable wrapper can have access to on every screen?
I just don't know where to start for these case.
Thank you for your time
EDIT:
I currently having the project structured in MVVM. For now, only one activity (MainActivity) is present, and inside that activity, the splash screen component or home screen or login screen,... are being navigated. So is it a good practice to create a viewmodel for the mainactivity screen, that can holds the color state for the theme?
Thanks #Maciej Ciemiega for the suggestion. I ended up structure my code like that.
In my MainActivity.kt I create a viewmodel for it.
val mainActivityViewModel by viewModels<MainActivityViewModel>()
MyTheme(mainActivityViewModel = mainActivityViewModel) {
initNavigationController(navController)
Surface(color = MaterialTheme.colors.background) {
if (mainActivityViewModel.appSettingsState.value.appSettings.colorsMapLight.size != 0
&& mainActivityViewModel.appSettingsState.value.appSettings.colorsMapDark.size != 0) {
navController.navigate(NavigationDestinations.homeScreen)
}
}
}
my initNavigationController function shows the splashscreen first. But it doesn't do anything. The getting app settings configuration is called in MyTheme composable via the mainActivityViewModel, and MyTheme will use the state from the viewmodel to define the theme, and the navController.navigate is based on the state as you guys can see in the if above.
I don't know if this is a good practice or not, or when my app grows it would be a mess or not, but at least it works for me. I tried with font styles too and it works like a charm.

How to force a LiveCard to be visible until dismissed

I'm building a heads up display for OBD data (speed, RPM, throttle position etc.) using Glass.
I am using a LiveCard to display the HUD, but like all cards it fades away after a few seconds as Glass puts the display to sleep.
Is there a way to force the card to remain visible until it's dismissed? A HUD display isn't very useful if it keeps needing to be woken up.
The code thus far is here: https://github.com/mpicco/glass-obd-hud.
Thanks,
Marty
I'd would recommend using an Immersion + the FLAG_KEEP_SCREEN_ON flag or getting a partial wakelock within your Service.
From the Android documentation:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();
// ..screen will stay on during this section..
wl.release();
If you do use the wakelock solution, do not forget to release it once your Service is stopped.
The way I've done this is inside the activity that I wish to keep the screen on for, I've added a window parameter like so:
public class MyActivity extends Activity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
The power manager service would likely work just as well, but this way when the user destroys my activity or other such things, I don't have to worry about maintaining state.

Unit testing android listview scrolling using Roboelectric

I have a method that attempts to programatically scroll to a position in a ListView. The method has some conditionals so that its implementation differs slightly based on the Android SDK version the app is running on.
The functionality works fine on 3 android devices that I have tested on. However, I have written a unit test using junit and Roboelectric that checks if the scrolling has made the the item at the target position visible. The test fails. When I debug, I notice that android.os.Build.VERSION.SDK_INT is 0 when running with Roboelectric (i.e., on the desktop vs. device or emulator).
I've tried ignoring the version and just using ListView.smoothScrollToPosition(), but the getFirstVisilePoition() and getLastVisiblePosition() methods continue to return 0, even immediately after I call smoothScrollToPosition(150).
Does anyone know if/how scrolling a listview can be tested using Roboelectric?
Any help would be appreciated - I can't seem to find any information on the topic.
Thanks,
Ana
If you're using robolectric 1.1 or 1.2, here is the source for the AbsListView:
https://github.com/pivotal/robolectric/blob/master/src/main/java/com/xtremelabs/robolectric/shadows/ShadowAbsListView.java
It looks like all the scrolling functionality is it ShadowAdapterView:
https://github.com/pivotal/robolectric/blob/master/src/main/java/com/xtremelabs/robolectric/shadows/ShadowAdapterView.java
It doesn't look like getFirstVisiblePoition() or getLastVisiblePosition() are implemented.
I was able to get the smooth scroll position.
#RunWith(RobolectricTestRunner.class)
public class SmoothScroll {
#Test
public void testSmoothScroll() {
Activity context = new Activity();
ListView view = new ListView(context);
view.smoothScrollToPosition(100);
Assert.assertEquals(100, Robolectric.shadowOf(view).getSmoothScrolledPosition());
}
}
You can get the smooth scrolled position. Based on the height of the elements, you may be able to work out which ones are visible.

JavaFx : Playing list of media files with pause in between

I am trying to play a list of mp3 files in JavaFx and need to pause for few seconds in between each file.This question may be subjective but I am unable to find any technique to get handle of another JavaFx MediaPlayer object at the end of currently playing mediaPlayer, which is being played inside a runnable object.
Any code sample/Algorithm will be a great help.
Create a list of mediaPlayers, iterate through the list and for each mediaPlayer:
Call mediaPlayer.setonEndofMedia.
In your end of media Runnable create a PauseTransition for the duration you want to pause.
Supply a setOnFinished event handler for the pause transition which starts the next MediaPlayer in the list.
Turn #SoulMan's comment into answer:
Thanks I used the following code:
PauseTransition pt = new PauseTransition(Duration.millis(2000));
pt.play();
pt.setOnFinished(
new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
if (mediaPlayer3 != null) {
mediaPlayer3.play();
}
}
});

Model View Controller Design pattern Code Example

I was studying the Model-View-Controller design pattern and i understand the concept behind the pattern theorotically, but I wanted to get a peek at how one would actually put it to practice.
Wikipedia mentions Wt - Web toolkit, CppCMS and some other standard implementations which use the pattern however I have not been familiar with these, and I was just hoping and
will be really grateful If anyone can provide some sample code(hopefully C++) which implements the pattern and explains the theory of the pattern being put to practice.
Here's a quick example I made (didn't try compiling it, let me know if there's errors):
class Button; // Prewritten GUI element
class GraphGUI {
public:
GraphGUI() {
_button = new Button("Click Me");
_model = new GraphData();
_controller = new GraphController(_model, _button);
}
~GraphGUI() {
delete _button;
delete _model;
delete _controller;
}
drawGraph() {
// Use model's data to draw the graph somehow
}
...
private:
Button* _button;
GraphData* _model;
GraphController* _controller;
};
class GraphData {
public:
GraphData() {
_number = 10;
}
void increaseNumber() {
_number += 10;
}
const int getNumber() { return _number; }
private:
int _number;
};
class GraphController {
public:
GraphController(GraphData* model, Button* button) {
__model = model;
__button = button;
__button->setClickHandler(this, &onButtonClicked);
}
void onButtonClicked() {
__model->increaseNumber();
}
private:
// Don't handle memory
GraphData* __model;
Button* __button;
};
Ignoring the implementation of Button, basically this program will use GraphGUI to display a graph that will change when a button is pressed. Let's say it's a bar graph and it will get taller.
Since the model is independent of the view (the button), and the controller handles the communication between the two, this follows the MVC pattern.
When the button is clicked, the controller modifies the model via the onButtonClicked function, which the Button class knows to call when it is clicked.
The beauty of this is since the model and view are completely independent, the implementation of each can drastically change and it won't affect the other, the controller might simply have to make a few changes. If the model in this case calculated some result based off some database data, then clicking the button could cause this to happen, but the button implementation wouldn't have to change. Or, instead of telling the controller when a click occurs, maybe it can tell the controller when the button is moused-over. The same changes are applied to model, regardless of what triggered the changes.
A simple text editor could be designed based on MVC. Think of the string class as the model, where data is stored. We might have a class called SimpleTextView which displays the text in the string attached to it, as it is. A class called KeyboardEventHandler can act as the controller. The controller will notify the view about new keyboard events. The view in turn modifies the model (like appending or removing text). The changes in the model is reflected on all views attached to it. For instance, there might be another view called HtmlView attached to the string object manipulated from within the SimpleTextView. If the user enters valid HTML tags in the SimpleTextView, the HtmlView will display the formatted output - real-time.
There are couple of complete MVC examples, plus discussion, in ch 2 of an introduction to programming in Python 3.x that I wrote (I've not completed ch 3 etc., that project's been on ice for some time -- Python community really like angry swarm of bees when discovered I'd written that Python was perhaps not suitable for very large scale development, so it became difficult to get sensible feedback). It's available in PDF format from Google Docs. I don't know how well it maps to common MVC implementations, I was mostly concerned with getting the general idea across. :-)
Cheers & hth.,
PS: There's a nice table of contents in the PDF file but Google Docs doesn't show it. You'd need to dl and use Foxit or Acrobat or some other PDF viewer. I think there's a separate viewable TOC at Google Docs, though, haven't checked and don't remember whether updated.
PPS: Forgot to mention, the MVC image processing example near the end has nice pic of Lena Söderberg! :)
Code is the best approach to understand and learn Model View Controller:
Here is a simple JS example (from Wiki)
/** Model, View, Controller */
var M = {}, V = {}, C = {};
/** Model stores data */
M.data = "hello world";
/** View controls what to present */
V.render = (M) => { alert(M.data); }
/** Controller bridges View and Model */
C.handleOnload = () => { V.render(M); }
/** Controller on Windows OnLoad event */
window.onload = C.handleOnload;
Here is a detailed post in C/C++
Model-View-Controller Explained in C++