Access ViewNavigatorApplication's actionbar component from View - android

I want to change icon of button thats in actioncontent of ViewNavigatorApplication from a view.
ViewNavigatorApplication loads views which are mxml components. i tried
Object(navigator.activeView).refreshbutton.icon = "../assets/r.gif";
It throws runtime error saying it couldn't find element.

Assuming your button is called refreshbutton and your app is called MyApp
MyApp(FlexGlobals.topLevelApplication).refreshbutton.icon="../assets/r.gif";
It's important to understand that your casting the top level application as your own app so it knows about the contents of your main level app including actionContent.
This isn't a very good development practice in general. I prefer to use a MVC pattern but that would be a very long answer.

Related

How to block Android app from being drawn on top of it? (Avoid Cloack&Dagger attacks)

Is there any way to tell my Android app that do not accept outside app content being drawn on top of it? Overlays,... (but of course accept application generated overlays, diglogs, toasts,...).
Specifically to avoid the kind of attacks such as Cloack and Dagger:
http://thehackernews.com/2017/05/android-hacking-technique.html?m=1
http://cloak-and-dagger.org/#Demos
Do full-screen and video apps fully avoid it? And if do, how do they do?
Set filterTouchesWhenObscured to true. Or implement the method onFilterTouchEventForSecurity().
More info here: https://blog.devknox.io/tapjacking-android-prevent/

React Native accessibility identifiers only on active scene

TalkBack is able to detect accessibility identifiers in my React Native 0.35.0 android app from non-active scenes I previously navigated through. How do I prevent this so only the current scene is used when identifying accessibility identifiers?
For example I have the first page of my app render a ListView of items. These work as expected with accessibility identifiers where any item can be selected.
The problem is when navigate to the next page, a page that only has one line of text, all the ListView items from the previous scene are still accessible when viewing the new scene!
I am using NavigationExperimental, specifically NavigationCardStack, for navigation. I'm aware NavigationCardStack will render each visited scenes in the stack but am looking to find a way so only the active scene is selectable.
So far I've tried implementing a version of NavigationExperimental that only renders the active scene. While it solves this problem the navigation animations are ruined and navigating back through the app must re-create each scene which creates a sluggish and unacceptable experience in my opinion.
While I don't love the solution I thought setting accessible={false} on every component from the first page may work. Unfortunately even this still left the Text components accessible. If it had worked I could have set accessible to NavigationExperimental's property scene.isActive.
I was able to resolve this issue. In React Native there is an attribute Android can set called importantForAccessibility. It's a little more intensive than I'd prefer but I implemented my own NavigationCardStack and NavigationCard (extended React Native's version to add my own logic in render).
Essentially while iterating over each scene in NavigationCardStack I pass a new property, isActiveScreen which I set to true only on the active scene by inspecting the current index.
Then in NavigationCard I leverage importantForAccessibility by adding
importantForAccessibility={this.props.isActiveScreen ? 'yes' : 'no-hide-descendants'}
In this way on android only my currently visible scene has accessibility enabled while all other scenes in the stack have no-hide-descendants set.
I have had the same issue using the react-navigation library. I was able to modify the react-navigation/src/views/CardStack/Card.jsin order to fix this which can be found here, I just modify the render method (Can't get the code to format :()
Card.js # Github
The following are my modifcations to this:
// Fixes an issue on Android whereby talkback/voiceover will pick up elements on a child view that is not active in the stack navigator
if(this.props.scene.isActive) {
importantForAccessibility='yes';
} else {
importantForAccessibility='no-hide-descendants';
}
return (
<Animated.View
pointerEvents={pointerEvents}
ref={this.props.onComponentRef}
style={[styles.main, style]}
importantForAccessibility={importantForAccessibility}
>
{children}
</Animated.View>
);
This fix has also been forked on github & published to NPM if anyone needs it as follows:
https://github.com/awseeley/react-navigation
https://www.npmjs.com/package/react-navigation-awseeley

How to handle UI based Navigation in Cross Platform Apps?

Assume that you have a cross platform application. The application runs on Android and on iOS. Your shared language across both platforms is Java. Typically you would write your business logic in Java and all you UI specific part in Java (for Android) and Objective-C (for iOS).
Typically when you implement the MVP pattern in a cross platform, cross language application you would have the Model and the Presenter in Java and provide a Java interface for your Views which is known to your presenters. This way your shared Java presenters can communicate with whatever view implementation you use on the platform specific part.
Lets assume we want to write a iOS app with a Java part which could be shared later on with the same Android app. Here is a graphical representation of the design:
On the left side there is the Java part. In Java you write your models, controllers as well as your view interfaces. You make all the wiring using dependency injection. Then the Java code can be translated to Objective-C using J2objc.
On the right side you have the Objective-C part. Here your UIViewController's can implement the Java interfaces which where translated into ObjectiveC protocols.
Problem:
What I am struggling about is how navigation between views takes place. Assume you are on UIViewControllerA and you tap a button which should bring you to UIViewControllerB. What would you do?
Case 1:
You report the button tap to the Java ControllerA (1) of UIViewControllerA and the Java ControllerA calls Java ControllerB (2) which is linked to UIViewControllerB (3). Then you have the problem that you do not know from the Java Controller side how to insert the UIViewControllerB in the Objective-C View hierarchy. You cannot handle that from the Java side because you have only access to the View interfaces.
Case 2:
You can make the transition to UIViewControllerB whether it is modal or with a UINavigationController or whatever (1). Then, first you need the correct instance of UIViewControllerB which is bind to the Java ControllerB (2). Otherwise the UIViewControllerB could not interact which the Java ControllerB (2,3). When you have the correct instance you need to tell Java ControllerB that the View (UIViewControllerB) has been revealed.
I am still struggling with this problem of how to handle the navigation between different controllers.
How can I model the navigation between different Controllers and handle the cross platform View changes appropriately?
Short answer:
Here is how we do it:
For simple "normal" stuff (like a button that opens the device camera or opens another Activity/UIViewController without any logic behind the action) - ActivityA directly opens ActivityB. ActivityB is now responsible communicating with the app shared logic layer if needed.
For anything more complex or logic dependent we're using 2 options:
ActivityA calls a method of some UseCase which returns an enum or public static final int and takes some action accordingly -OR-
Said UseCase can call a method of a ScreenHandler we registered earlier which knows how to open common Activities from anywhere in the app with some supplied parameters.
Long answer:
I'm the lead developer in a company using a java library for the application's models, logic, and business rules which both mobile platforms (Android and iOS) implement using j2objc.
My design principles come directly from Uncle Bob and SOLID, I really dislike the usage of MVP or MVC when designing whole applications complete with inter-component communications because then you start linking each Activity with 1 and only 1 Controller which sometimes is OK but most of the times you end up with a God Object of a controller that tends to change as much as a View. This can lead to serious code smells.
My favorite way (and the one I find cleanest) of handling this is breaking everything up into UseCases each of which handles 1 "situation" in the app. Sure you can have a Controller that handles several of those UseCases but then all it knows is how to delegate to those UseCases and nothing more.
Additionally, I don't see a reason of linking every action of an Activity to a Controller sitting in the logical layer, if this action is a simple "take me to the map screen" or anything of this sort. The role of the Activity should be handling the Views it holds, and as the only "smart" thing living in the life cycle of the application, I see no reason it can't call the start of the next activity itself.
Furthermore, Activity/UIViewController lifecycle is too complex and too different from each other to be handled by the common java lib. It is something I view as a "detail" and not really "business rules", each platform needs to implement and worry about, thus making the code in the java lib more solid and not prone to change.
Again, my goal is to have each component of the app be as SRP (Single Responsibility Principle) as it can be, and this means linking as few things together as possible.
So an example of simple "normal" stuff:
(all examples are totally imaginary)
ActivityAllUsers displays a list of model object items. Those items came from calling AllUsersInteractor - a UseCase controllerin a back thread (which in turn also handled by the java lib with a dispatch to main thread when the request is completed). The user clicks on one of the items in this list. In this example the ActivityAllUsers already has the model object so opening ActivityUserDetail is a straightforward call with a bundle (or another mechanism) of this data model object. The new activity, ActivityUserDetail, is responsible of creating and using the correct UseCases if further actions are needed.
Example of complex logic call:
ActivityUserDetail has a button titled "Add as a friend" which when clicked calls the callback method onAddFriendClicked in the ActivityUserDetail:
public void onAddFriendClicked() {
AddUserFriendInteractor addUserFriend = new AddUserFriendInteractor();
int result = addUserFriend.add(this.user);
switch(result){
case AddUserFriendInteractor.ADDED:
start some animation or whatever
break;
case AddUserFriendInteractor.REMOVED:
start some animation2 or whatever
break;
case AddUserFriendInteractor.ERROR:
show a toast to the user
break;
case AddUserFriendInteractor.LOGIN_REQUIRED:
start the log in screen with callback to here again
break;
}
}
Example of even more complex call
A BroadcastReceiver on Android or AppDelegate on iOS receive a push notification. This is sent to NotificationHandler which is in the java lib logical layer. In the NotificationHandler constructor which is constructed once in the App.onCreate() it takes a ScreenHandler interface which you implemented in both platforms. This push notification is parsed and the correct method is called in the ScreenHandler to open the correct Activity.
The bottom line is: keep the View as dumb as you can, keep the Activity just smart enough to handle its own life cycle and handle its own views, and communicate with its own controllers (plural!), and everything else should be written (hopefully test-first ;) ) in the java lib.
Using these methods our app currently runs about 60-70% of its code in the java lib, with the next update should take it to the 70-80% hopefully.
I would recommend that you use some kind of slot mechanism. Similar to what other MVP frameworks use.
Definition: A slot is a part of a view where other views can be inserted.
In your presenter you can define as many slots as you want:
GenericSlot slot1 = new GenericSlot();
GenericSlot slot2 = new GenericSlot();
GenericSlot slot3 = new GenericSlot();
These slots must have a reference in the Presenter's view. You can implement a
setInSlot(Object slot, View v);
method. If you implement setInSlot in a view then the view can decide how it should be included.
Have a look at how slots are implemented here.
In cross platform development sharing what I call a "core" (the domain of your application written in Java), I tend to give to the UI ownership of which view to display next. That makes your application more flexible, adapting to the environment as needed (Using a UINavigationController on iOS, Fragments on Android and a single page with dynamic content on the web interface).
Your controllers should not be tied to a view, but instead fulfill a specific role (an accountController for logins/logouts, a recipeController for displaying and editing a recipe, etc).
You would have interfaces for your controllers instead of your views. Then you could use the Factory design pattern to instantiate your controllers on the domain side (your Java code), and the views on the UI side. The view factory has a reference to your domain's controller factory, and uses it to give to the requested view some controllers implementing specific interfaces.
Example:
Following a tap on a "login" button, a homeViewController asks the ViewControllerFactory for a loginViewController. That factory in turn asks the ControllerFactory for a controller implementing the accountHandling interface. It then instantiates a new loginViewController, gives it the controller it just received, and returns that freshly instantiated view controller to the homeViewController. The homeViewController then presents that new view controller to the user.
Since your "core" is environment-agnostic and only contains your domain & business logic, it should remain stable and less prone for edits.
You could take a look at this simplified demo project I made which illustrates this set-up (minus the interfaces).

Native ActionBar-based tabs with native fragments (with MvvmCross)

I am porting an Xamarin-based Android app to be PCL'ified and MvvmCross'ified so we can target multiple mobile device platforms.
Since this is a line-of-business app where we can dictate the minimum Android OS version, we wish to avoid using the various v4 / v7 etc "Support libraries". We prefer to use all native components that are already part of the OS. So this basically means we only wish to target Jellybean and probably Android v4.2 upwards.
Unfortunately whilst there are lots of examples out there of using MvvmCross with ActionBarSherlock and the SupportActionBar (in a support library), there appears to be no examples of how to wire up MvvmCross with the native ActionBar using native Fragments as tabs.
I'm not expecting someone to post up swathes of code but would really appreciate if someone could give me a few pointers on the best approach to take to achieve this.
I will certainly blog about it once I have a working solution to benefit the whole MvvmCross/Xamarin community.
Thanks.
Probably the most important thing is to use MvvmCross - Hot Tuna Droid Full Fragment Support NUGet library. Then create activity that inherits from MvxActivity and fragment from MvxFragment.
ViewModel for tab fragment set in activity when create this class for TabEventArgs.FragmentTransaction.Add method used in ActionBar.Tab.TabSelected event handler like this:
var tab1 = new TabFragment1 {DataContext = ViewModel};
In TabFragment1 class you can use standard fluent binding method on bindingSet creted by
var bindingSet = this.CreateBindingSet<TabFragment1, MyViewModel>();
I don't know if it is a right way, but work.

Android Graphical UI Builders - Connect event

Maybe this question has been ask already, but could not find any answer for almost 2hours of internet search.
There is a graphical UI designer wich is coming along with the last android SDK.
Looks pretty cool and well done.
Nevertheless I * cannot find how to attach an event to the control through the graphical editor.
Of course I can add it manually into the xml, but in that case, what's the purpose of having such tool without that function ?
I mean all the other SDK I had in other languages always include that function.
I've also not been able to find doc about how to use this tool. Quite sad...
Thanks
If you want to add a click event handler, select the button (widget) in the GUI that you want to listen for, and look for the property onClick. Enter the name of the method you want to call when the user clicks on that widget, like.. onMyButtonClick
Then add the method to your Activity
public void onMyButtonClick(View v) {
// I heard the button click
}
The GUI builder is getting there, and is not yet as easy to use as the one in XCode, but it's not hard when you get used to it.

Categories

Resources