My app has one activity. The app has a drawer that has a list that is filled from my content provider. From the drawer the user can select an item and then the Activity will be filled with the appropriate content dynamically. I am not sure how to implement app indexing in such a case. I mean based on step 3 of the tutorial, the activity seems to be expected to show one content (am I wrong about this)?
Note: I already got deep linking working ( I have a website and the content map to the content in the app).
Specifically I am wondering to I dynamically change the following each time the user changes the content:
mUrl = "http://examplepetstore.com/dogs/standard-poodle";
mTitle = "Standard Poodle";
mDescription = "The Standard Poodle stands at least 18 inches at the withers";
And if yes, how about the fact that I am only supposed to make the call once (in onStart only). And again, my data is loaded from a content provider. The provider itself is loaded from the server, but that call loads everything -- as opposed to just loading a single page.
AFAIK, you should connect your GoogleApiClient once per activity only. However, you can index your dynamical content as much as you want (but better to not index content too many times), just remember to disconnect them when your activity finish. Below is what I did in my project:
HashMap<String, Action> indexedActions;
HashMap<String, Boolean> indexedStatuses;
public void startIndexing(String mTitle, String mDescription, String id) {
if (TextUtils.isEmpty(mTitle) || TextUtils.isEmpty(mDescription))
return; // dont index if there's no keyword
if (indexedActions.containsKey(id)) return; // dont try to re-indexing
if (mClient != null && mClient.isConnected()) {
Action action = getAction(mTitle, mDescription, id);
AppIndex.AppIndexApi.start(mClient, action);
indexedActions.put(id, action);
indexedStatuses.put(id, true);
LogUtils.e("indexed: " + mTitle + ", id: " + id);
} else {
LogUtils.e("Client is connect : " + mClient.isConnected());
}
}
public void endIndexing(String id) {
// dont endindex if it's not indexed
if (indexedStatuses.get(id)) {
return;
}
if (mClient != null && mClient.isConnected()) {
Action action = indexedActions.get(id);
if (action == null) return;
AppIndex.AppIndexApi.end(mClient, action);
indexedStatuses.put(id, false);
}
}
Related
Let's say I have a similar scenario:
I'm using an accessibility service in order to my TTS engine talk when this dialog appears, but the only thing I was able to detect were the selectable views (those pointed by the arrow).
Is there any way to detect the title and (more importantly) whole text inside the dialog?
Yes. I think it is likely that you're grabbing these items off of accessibility events, which focus on a single node. What you want to do instead is look at the entire view hierarchy. You can do this one of two ways. First thing to note is that Accessibility Nodes are a tree. Just like the view heirarchy is a tree. In fact, this tree matches the view hierarchy, almost 1 to 1. Developers can force an element to not be included in the view hierarchy, though this isn't done often in practice. Even if they do you can get this information regardless. Let's assume we want this information. First thing we want to do is make sure it's included.
protected void onServiceConnected() {
super.onServiceConnected();
AccessibilityServiceInfo tempInfo = getServiceInfo();
tempInfo.flags |= AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
setServiceInfo(tempInfo);
}
It's highly likely you can skip this step, but just in case!
After this it's quite simple. First, so that you can see where this information is, let's write a cute little logging function.
public static void logNodeHeirarchy(AccessibilityNodeInfo nodeInfo, int depth) {
if (nodeInfo == null) return;
String logString = "";
for (int i = 0; i < depth; ++i) {
logString += " ";
}
logString += "Text: " + nodeInfo.getText() + " " + " Content-Description: " + nodeInfo.getContentDescription();
Log.v(LOG_TAG, logString);
for (int i = 0; i < nodeInfo.getChildCount(); ++i) {
logNodeHeirarchy(nodeInfo.getChild(i), depth + 1);
}
}
This function should log the entire tree of an accessibility node. Add it as a static function to your accessibility service. Now we just need to call it on the root node. You can easily change the properties that get logged. I find text, content description, and view id to be the most useful.
#Override
public void onAccessibilityEvent(AccessibilityEvent e) {
switch (e.getEventType()) {
case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED: {
logNodeHeirarchy(getRootInActiveWindow(), 0);
}
}
}
This should allow you to see where the information is. All you have to do is figure out how to parse it. Note: You can also crawl up from a leaf node, using getParent().
I have a couple questions of how to implement google game services in my app.
First of all, after I checked that the user is signed in and the client is connected, I launch
Intent intent = Games.TurnBasedMultiplayer.getSelectOpponentsIntent(mGoogleApiClient, 1, 1, true);
startActivityForResult(intent, RC_SELECT_PLAYERS);
To show the user a dialog where they can select the opponent.
After I get the result from the activity I execute this code
if (request == RC_SELECT_PLAYERS) {
Log.e(LOG_TAG, "Got RC_SELECT_PLAYERS result intent");
if (response != Activity.RESULT_OK) {
// user canceled
return;
}
// Get the invitee list.
final ArrayList<String> invitees = data.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);
// Get auto-match criteria.
Bundle autoMatchCriteria;
int minAutoMatchPlayers = data.getIntExtra(Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
int maxAutoMatchPlayers = data.getIntExtra(Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);
if (minAutoMatchPlayers > 0) {
autoMatchCriteria = RoomConfig.createAutoMatchCriteria(minAutoMatchPlayers, maxAutoMatchPlayers, 0);
} else {
autoMatchCriteria = null;
}
TurnBasedMatchConfig turnBasedMatchConfig = TurnBasedMatchConfig.builder()
.addInvitedPlayers(invitees)
.setAutoMatchCriteria(autoMatchCriteria)
.build();
// Create and start the match.
Games.TurnBasedMultiplayer
.createMatch(mGoogleApiClient, turnBasedMatchConfig)
.setResultCallback(new MatchInitiatedCallback());
}
To initiate the match. The problem is, the opponent I challenged doesn't get any notification where he is asked to join the game. So when the user gets a notification to join a game, how can I catch this notification and show the user the appropriate content? Which intent result should I look for?
In my MatchInitiatedCallback I check which turn it is now:
public class MatchInitiatedCallback implements ResultCallback<TurnBasedMultiplayer.InitiateMatchResult> {
private static final String LOG_TAG = "MatchInitiatedCallback";
#Override
public void onResult(TurnBasedMultiplayer.InitiateMatchResult result) {
// Check if the status code is not success.
Status status = result.getStatus();
if (!status.isSuccess()) {
Log.e(LOG_TAG, "showError() " + status.toString());
//showError(status.getStatusCode());
return;
}
TurnBasedMatch match = result.getMatch();
// If this player is not the first player in this match, continue.
if (match.getData() != null) {
Log.e(LOG_TAG, "Not my turn");
//showTurnUI(match);
return;
}
// Otherwise, this is the first player. Initialize the game state.
//initGame(match);
Log.e(LOG_TAG, "initGame()");
// Let the player take the first turn
//showTurnUI(match);
Log.e(LOG_TAG, "showTurnGui()");
}
}
But this code gets executed even before my opponent selects to join.
Shouldn't he first be able to accept or decline the game?
Can someone please tell me if I'm doing this properly or if this is supposed to be like this? If not, how should it be implemented correctly?
EDIT: I took all this code from the documentation. It does show some code samples of how to implement things, but not of how the code all comes together and the order in which things should be executed, or how to handle notifications and such.
Yes, it is supposed to work like this. You must take a turn before the opponent actually receives the invitation. In fact, if you invite several opponents, each of them will have to accept the invitation and take a turn before the next opponent is invited, meaning it can take quite a while before you know whether you actually got a match going.
To avoid asking the users to take turns during the invitation phase, your program can take a dummy turn for each user to pass the invitation to the next user. When all users have joined you can start to take real turns.
So I'm trying to do the following in android xamarin.
When you press on a map element an infowindow is shown.When you press that window and an event is linked to the element the app goes to another fragment that is described by the action object. The problem is the moment I press the infowindow the whole app freezes and nothing happens. In the logs I can't see anything and the app stops at the following line:
pager.Adapter = pagerAdapter;
Added a breakpoint there and after saying "step over" the ide doesn't break anymore and the app freezes (no user interaction possible).
So let me start by giving all the relative code and a little explanation.
So first I'll show you what happens on the infowindow click. This happens on a SupportMapFragment that has it's own listener.
void GoogleMap.IOnInfoWindowClickListener.OnInfoWindowClick (Marker p0)
{
InfoPopup ip = CustomJsonConverter.Convert<InfoPopup> (p0.Title);
if (ip == null || ip.Goto == null || !(this.Activity is MainView))
return;
MainView main = (this.Activity as MainView);
p0.HideInfoWindow ();
switch (ip.Goto.type) {
case "InfoFragment":
Info info = InfoController.Items.Find (x => x.Index == ip.Goto.id);
if (info != null) {
main.RunOnUiThread (() => {
main.ShowInfosFragment ();
main.ShowInfoFragment (info);
});
}
break;
case "FaqFragment":
FaQ faq = FaQController.Items.Find (x => x.Index == ip.Goto.id);
if (faq != null) {
main.RunOnUiThread (() => {
main.ShowFaqsFragment ();
main.ShowFaqFragment (faq);
});
}
break;
}
}
I tested it with an action of InfoFragment which gives back an item so that's good. Then it goes to the main.ShowInfosFragment() which is where it freezes. So that method is really simple. This function is in the Activity holding the fragments (thanks Cheesebaron).
public void ShowInfosFragment(){
ShowFragment(1, new InfosFragment (){ InfoSelectedAction = ShowInfoFragment });
}
So the problem giving function is the following. This function is in the Activity holding the fragments (thanks Cheesebaron).
protected void ShowFragment(int index, Android.Support.V4.App.Fragment fragment, bool ignoreType = false){
RemoveSubMenu();
if (pagerAdapter.Count <= index || ignoreType || !(pagerAdapter.GetItem (index).GetType().Equals(fragment.GetType()))) {
pagerAdapter.AddFragment (index, fragment);
SetSubMenu (fragment);
pager.Adapter = pagerAdapter;
}
pager.SetCurrentItem (index, true);
DrawerButtonLayout ();
}
I've used this function a lot and it has always worked to move to the other fragments from the menu or at startup to set the mapfragment.
Anyone sees what's the problem with my code? Tried tons of things but can't seem to figure this one out with my friend google.
Thanks already for reading.
Kind regards,
Karl
With Google Play Game Services, I'm trying to implement a CallBack such that, if there is an issue with sending a message, then I need to resolve it (as each player "passes" their bid to the next player, and all other players need to see what the player that passed bid)
I thought I would try and use the following to instantiate a RealTimeReliableMessageSentListener for that round of messages, so that I can tell if the message was sent and received by everyone:
(I add the tokenID returned by the call to an ArrayList, and then check off remove each tokenID as it comes back in to track when all messages from this round are received)
#Override
public void sendReadyToPlay() {
dLog("Sending ReadyToPlay");
// Broadcast that I'm ready, and see that they are ready
if (!mMultiplayer){
return; // playing against computer
}
// First byte in message indicates whether it's a final score or not
mMsgBuf[0] = (byte) ('R');
readyToPlayTokens.clear();
// Send to every other participant.
for (Participant p : mParticipants) {
dLog("Participant:" + p.getParticipantId());
if (p.getParticipantId().equals(mMyId)) {
continue;}
if (p.getStatus() != Participant.STATUS_JOINED){
continue;
}
readyToPlayTokens.add(mHelper.getGamesClient().sendReliableRealTimeMessage(new RealTimeReliableMessageSentListener() {
#Override
public void onRealTimeMessageSent(int statusCode, int tokenId, String recipientParticipantId){
dLog("onRealTimeMessageSent number two and size is: " + readyToPlayTokens.size());
if(readyToPlayTokens.contains(tokenId)){
readyToPlayTokens.remove(tokenId);
}
dLog("onRealTimeMessageSent number two and size is: " + readyToPlayTokens.size());
if (statusCode != GamesClient.STATUS_OK) {
mGHInterface.onRealTimeMessageReceived("RTPProblem:" + recipientParticipantId);
} else if (readyToPlayTokens.size() == 0) {
mGHInterface.beginRound();
}
}
}, mMsgBuf, mRoomId, p.getParticipantId()));
dLog("sent to:" + p.getParticipantId());
}
}
I can see the messages coming in almost every time from one device to another, so I can see that the messages are going through, BUT, the RealTimeReliableMessageSent listener is only being fired about 50-60 percent of the time, which isn't very reliable! :)
Can anyone see what I might be doing wrong to keep the listener from firing reliably?
may be it happens because you are using anonymous inner class, try to use in different way like implementing your activity RealTimeReliableMessageSentListener
EXTRA_APPLICATION_ID seems not to work in the tablet. Do you know other work around to reuse tab of the Browser.apk when starting the Browser by intent from my service. This is directly related to 9902225. Any hints is appreciated.
Cause: The resuseTab() will not be called if the device is a tablet.
See line:
* 3-tablet) Open new tab
See line:
if (activateVoiceSearch || !BrowserActivity.isTablet(mActivity)) {
// Browser.apk source code (Android 4.0.3_r1-6)
com.android.browser.IntentHandler
void onNewIntent(Intent intent) {
....
/*
* TODO: Don't allow javascript URIs
* 0) If this is a javascript: URI, *always* open a new tab
* 1) If this is a voice search, re-use tab for appId
* If there is no appId, use current tab
* 2) If the URL is already opened, switch to that tab
* 3-phone) Reuse tab with same appId
* 3-tablet) Open new tab
*/
final String appId = intent
.getStringExtra(Browser.EXTRA_APPLICATION_ID);
if (!TextUtils.isEmpty(urlData.mUrl) &&
urlData.mUrl.startsWith("javascript:")) {
// Always open javascript: URIs in new tabs
mController.openTab(urlData);
return;
}
if ((Intent.ACTION_VIEW.equals(action)
// If a voice search has no appId, it means that it came
// from the browser. In that case, reuse the current tab.
|| (activateVoiceSearch && appId != null))
&& !mActivity.getPackageName().equals(appId)) {
if (activateVoiceSearch || !BrowserActivity.isTablet(mActivity)) {
Tab appTab = mTabControl.getTabFromAppId(appId);
if (appTab != null) {
mController.reuseTab(appTab, urlData);
return;
}
}
// No matching application tab, try to find a regular tab
// with a matching url.
Tab appTab = mTabControl.findTabWithUrl(urlData.mUrl);
if (appTab != null) {
// Transfer ownership
appTab.setAppId(appId);
if (current != appTab) {
mController.switchToTab(appTab);
}
// Otherwise, we are already viewing the correct tab.
} else {
// if FLAG_ACTIVITY_BROUGHT_TO_FRONT flag is on, the url
// will be opened in a new tab unless we have reached
// MAX_TABS. Then the url will be opened in the current
// tab. If a new tab is created, it will have "true" for
// exit on close.
Tab tab = mController.openTab(urlData);
if (tab != null) {
tab.setAppId(appId);
if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
tab.setCloseOnBack(true);
}
}
}
For ICS and tablet devices, you need to set the intent's App ID as the browser's App ID.
So you need to use com.android.browser instead of getPackageName()