Categories
discuss

android: how to remove the back/home button in the action bar

I am having difficulties trying to remove the back/home button from the action bar.

 getActionBar().setDisplayShowHomeEnabled(false);   //disable back button
 getActionBar().setHomeButtonEnabled(false);

In a older android phone, the back button is removed with these two code lines. However with the nexus 4, the back button still appears but is just disabled. Also I am just adding a menu item on the right that behaves like the back/home button replacing the back/home button. What am I missing?

Answer

Use getActionBar().setDisplayHomeAsUpEnabled(false) to remove the home button from the action bar.

Categories
discuss

Angular-ui Router get two states active in parallel

I need to use two states in parallel, one for my page and an other for a modal with several sub states. Right now calling the modal state will wipe out my page since the page state changed.

Create a child state child of my page wouldn’t be a solution since the modal will be used on several pages.

Example:

$stateProvider
.state('user', {}) // page
.state('bookshelf', {}) // page
.state('books', {}) // modal
.state('books.read', {}) // sub state of modal

So if I’m on user and open my modal then the state would change to books, my modal would then have the content but the page content will be wiped out.

How do I fix it?

Answer

I believe the way you’re looking to do this is not possible with UI.Router currently. What you’re describing is a modal component (which would ideally be written as a directive), which tracks it’s state independently from the main state.

The way to think about it, is that UI.Router works by creating a state tree. At any given time you can only be looking at one branch of the tree. You can go deeper down a branch (ie: book, book.open, book.open.checked), but you can’t be in two places at once.

Another issue with the problem above is how do you serialize the state of the two different trees into one url? It’s not to say it can’t be done, it’s just a hard problem to solve.

Checkout these issues:

Also checkout these repos, they might be further along the lines of solving the problem.

As far as solving your immediate problem, I think the ‘easiest’ way would be to ditch controlling the state of the modal inside your state config.

Instead, I would add some sort of root or abstract state, and then track whether the modal is open there. Then, you can communicate between controllers using events as shown here. Note: There are performance implications with listening to $rootScope, so be sure to research those. However (someone feel free to correct me), the implementation here doesn’t have those problems, because the AppCtrl is never destroyed.


Jan 15, 2015 Edit

Turns out this is a pretty popular use case, and one of the core contributors to UI Router maintains a plugin/addition called UI Router Extras

It also includes utilities for lazy loading, called “Future States” which are very helpful.

That being said, one feature I’m hoping to get time to work on is maintaining all state within the URL (or perhaps, local storage) and allowing for reusable state “components”. The latter is in the UI Router roadmap as well.

Categories
discuss

Error when starting a new activity Android

I’m new in Android developing and I’m having some issues with creating a new Activity. I want to start a new Activity when a Button is clicked but the app crashes. Here is what is said in the console and in the logcat:

LogCat

03-11 00:21:08.639: D/AndroidRuntime(336): Shutting down VM
03-11 00:21:08.649: W/dalvikvm(336): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-11 00:21:08.659: E/AndroidRuntime(336): FATAL EXCEPTION: main
03-11 00:21:08.659: E/AndroidRuntime(336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.src.vicnote/com.src.vicnote.MainActivity}: java.lang.NullPointerException
03-11 00:21:08.659: E/AndroidRuntime(336):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-11 00:21:08.659: E/AndroidRuntime(336):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-11 00:21:08.659: E/AndroidRuntime(336):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-11 00:21:08.659: E/AndroidRuntime(336):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-11 00:21:08.659: E/AndroidRuntime(336):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 00:21:08.659: E/AndroidRuntime(336):  at android.os.Looper.loop(Looper.java:123)
03-11 00:21:08.659: E/AndroidRuntime(336):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-11 00:21:08.659: E/AndroidRuntime(336):  at java.lang.reflect.Method.invokeNative(Native Method)
03-11 00:21:08.659: E/AndroidRuntime(336):  at java.lang.reflect.Method.invoke(Method.java:507)
03-11 00:21:08.659: E/AndroidRuntime(336):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-11 00:21:08.659: E/AndroidRuntime(336):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-11 00:21:08.659: E/AndroidRuntime(336):  at dalvik.system.NativeStart.main(Native Method)
03-11 00:21:08.659: E/AndroidRuntime(336): Caused by: java.lang.NullPointerException
03-11 00:21:08.659: E/AndroidRuntime(336):  at com.src.vicnote.MainActivity.onCreate(MainActivity.java:26)
03-11 00:21:08.659: E/AndroidRuntime(336):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-11 00:21:08.659: E/AndroidRuntime(336):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-11 00:21:08.659: E/AndroidRuntime(336):  ... 11 more
03-11 00:21:38.579: D/AndroidRuntime(365): Shutting down VM
03-11 00:21:38.579: W/dalvikvm(365): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-11 00:21:38.599: E/AndroidRuntime(365): FATAL EXCEPTION: main
03-11 00:21:38.599: E/AndroidRuntime(365): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.src.vicnote/com.src.vicnote.MainActivity}: java.lang.NullPointerException
03-11 00:21:38.599: E/AndroidRuntime(365):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-11 00:21:38.599: E/AndroidRuntime(365):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-11 00:21:38.599: E/AndroidRuntime(365):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-11 00:21:38.599: E/AndroidRuntime(365):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-11 00:21:38.599: E/AndroidRuntime(365):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 00:21:38.599: E/AndroidRuntime(365):  at android.os.Looper.loop(Looper.java:123)
03-11 00:21:38.599: E/AndroidRuntime(365):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-11 00:21:38.599: E/AndroidRuntime(365):  at java.lang.reflect.Method.invokeNative(Native Method)
03-11 00:21:38.599: E/AndroidRuntime(365):  at java.lang.reflect.Method.invoke(Method.java:507)
03-11 00:21:38.599: E/AndroidRuntime(365):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-11 00:21:38.599: E/AndroidRuntime(365):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-11 00:21:38.599: E/AndroidRuntime(365):  at dalvik.system.NativeStart.main(Native Method)
03-11 00:21:38.599: E/AndroidRuntime(365): Caused by: java.lang.NullPointerException
03-11 00:21:38.599: E/AndroidRuntime(365):  at com.src.vicnote.MainActivity.onCreate(MainActivity.java:26)
03-11 00:21:38.599: E/AndroidRuntime(365):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-11 00:21:38.599: E/AndroidRuntime(365):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-11 00:21:38.599: E/AndroidRuntime(365):  ... 11 more

activity_main

<pre><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.src.vicnote.MainActivity"
tools:ignore="MergeRootFrame" /><code>

fragment_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/MainScreen" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.src.vicnote.MainActivity$PlaceholderFragment" > <Button android:id="@+id/buttonNew" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="New" /> </RelativeLayout>

Java code

http://pastebin.com/K8eAWD2L

Answer

from what it looks like is your setting the content view

setContentView(R.layout.activity_main);

but there is no button in activity_main.xml from what you have provided and it is showing a nullpointerexception because

newButton = (Button) findViewById(R.id.buttonNew);

is null because that actual button from the information you provided is in

fragment_main.xml

so you can try

setContentView(R.layout.fragment_main);
Categories
discuss

Encoding user input to be stored in MongoDB

I’m trying to determine the best practices for storing and displaying user input in MongoDB. Obviously, in SQL databases, all user input needs to be encoded to prevent injection attacks. However, my understanding is that with MongoDB we need to be more worried about XSS attacks, so does user input need to be encoded on the server before being stored in mongo? Or, is it enough to simply encode the string immediately before it is displayed on the client side using a template library like handlebars?

Here’s the flow I’m talking about:

  1. On the client side, user updates their name to “<script>alert(‘hi’);</script>”.
    • Does this need to be escaped to “&lt;script&gt;alert(&#x27;hi&#x27;);&lt;&#x2F;script&gt;” before sending it to the server?
  2. The updated string is passed to the server in a JSON document via an ajax request.
  3. The server stores the string in mongodb under “user.name”.
    • Does the server need to escape the string in the same way just to be safe? Would it have to first un-escape the string before fully escaping so as to not double up on the ‘&’?
  4. Later, user info is requested by client, and the name string is sent in JSON ajax response.
  5. Immediately before display, user name is encoded using something like _.escape(name).

Would this flow display the correct information and be safe from XSS attacks? What about about unicode characters like Chinese characters?

This also could change how text search would need to be done, as the search term may need to be encoded before starting the search if all user text is encoded.

Thanks a lot!

Answer

Does this need to be escaped to "&lt;script&gt;alert(&#x27;hi&#x27;);&lt;&#x2F;script&gt;" before sending it to the server?

No, it has to be escaped like that just before it ends up in an HTML page – step (5) above.

The right type of escaping has to be applied when text is injected into a new surrounding context. That means you HTML-encode data at the moment you include it in an HTML page. Ideally you are using a modern templating system that will do that escaping for you automatically.

(Similarly if you include data in a JavaScript string literal in a <script> block, you have to JS-encode it; if you include data in in a stylesheet rule you have to CSS-encode it, and so on. If we were using SQL queries with data injected into their strings then we would need to do SQL-escaping, but luckily Mongo queries are typically done with JavaScript objects rather than a string language, so there is no escaping to worry about.)

The database is not an HTML context so HTML-encoding input data on the way to the database is not the right thing to do.

(There are also other sources of XSS than injections, most commonly unsafe URL schemes.)

Categories
discuss

Regex parsing of CSS @media queries (and other nested selectors)

I’m working on a node script which uses regex to parse CSS files, and it works perfectly… except when dealing with @media queries. The problem is due to the nested curly-brackets which are giving me fits. I essentially want to create a capturing group of ALL the content inside a media query: Here’s what I’ve got so far.

@media[^{]+{([^}]+)}s*}

This works fine on something simple like:

@media (max-width: 868px) {
  aside .size-toggle {
    display: none;
  }
}

But can’t pick up multiple nested rules, like this:

@media (max-width: 767px) {
  #wrapper.sidebar-display aside {
    left: 0;
    transition: all 0.5s ease;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -ms-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
  }
  #wrapper.sidebar-display #top-nav {
    left: 0;
    right: -194px;
  }
}

How do I need to modify my regex so that the capturing group contains all the selectors and rules inside each individual @media query?

Answer

Try this regex:

/@media[^{]+{([sS]+?})s*}/g

Demo

http://regex101.com/r/iT2eR5

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..