Categories
discuss

How to trigger MediaScan on Nexus 7?

Because I want to make sure the MediaStore has the latest information without having to reboot I’d like to trigger the MediaScanner using the popular way I found on SO

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                                 Uri.parse("file://" + Environment.getExternalStorageDirectory())));

This works fine on my Samsung S2 w/ICS but not on my Nexus 7 w/JellyBean. Logcat shows this on my Nexus 7:

WARN/ActivityManager(480): Permission denied: checkComponentPermission() owningUid=10014
WARN/BroadcastQueue(480): Permission Denial: broadcasting Intent { act=android.intent.action.MEDIA_MOUNTED dat=file:///storage/emulated/0 flg=0x10 } from com.example.foo.bar (pid=17488, uid=10046) is not exported from uid 10014 due to receiver com.android.providers.downloads/.DownloadReceiver
INFO/ActivityManager(480): Start proc com.google.android.music:main for broadcast com.google.android.music/.store.MediaStoreImportService$Receiver: pid=17858 uid=10038 gids={50038, 3003, 1015, 1028}
INFO/MusicStore(17858): Database version: 50
INFO/MediaStoreImporter(17858): Update: incremental Added music: 0 Updated music: 0 Deleted music: 0 Created playlists: 0 Updated playlists: 0 Deleted playlists: 0 Inserted playlist items: 0 Deleted playlist items: 0 Removed orphaned playlist items: 0

The last line sounds encouraging in theory, but the values are always 0 even after new files had been pushed to the SD card (via adb push). On my older device (S2) it does remount the SD card.

I’ve added the following permissions to my AndroidManifest.xml but it behaves the same as without those permissions:

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Any ideas/alternatives?


Edit 1:

Note that I don’t know any file paths of new or modified or deleted files. I just want to make sure the MediaStore is up-to-date.

Answer

using the popular way I found on SO

Faking ACTION_MEDIA_MOUNTED broadcasts has never been an appropriate solution IMHO.

Any ideas/alternatives?

Use MediaScannerConnection, such as via its scanFile() static method.

Categories
discuss

Struts 2 OGNL – Comparing two string values in validation.xml

I am new to Struts2 and OGNL and am making a simple web application with a registration page. There are two fields, password and repassword (to re-enter the password) and using the validation framework i would like to validate that the two passwords match (I know that I can do it easily with JavaScript). Here is what I’ve got so far. All of the field-validators are working fine. This is my first non-field validator and I just cant get it to work.

<validator type="expression">
    <param name="expression">${password}!=${repassword}</param>
    <message>Passwords must match.</message>
</validator> 

I tried both with

${password}!=${repassword}

and without

password!=repassword

the OGNL tags.

Answer

The expression validator is a Non-Field Level validator. Use fieldexpression validator which is a Field Level validator and validates using OGNL expression. And it must be equals (==) check.

<field name="password">
  <field-validator type="fieldexpression">
    <param name="expression"><![CDATA[password == repassword]]></param>
    <message>Passwords must match.</message>
  </field-validator>
</field>

The expression validator adds action errors. The fieldexpression validator adds field errors.

Categories
discuss

Dialog Fragment with and EditText shows keyboard automatically after load

I have a DialogFragment that has a view with an EditText in it. Every time I show the dialog the edittext has focus and the keyboard comes up.

How can i stop it from coming up automatically?

I tried putting android:windowSoftInputMode="stateHidden" in my manifest for the activity that creates the fragment but that had no effect

this is my dialog fragment

public class RegDialog extends DialogFragment {

OnRegComplete mRegComplete;

@Override
public void onAttach(Activity activity){
    super.onAttach(activity);
    try{
        mRegComplete = (OnRegComplete)activity;
    }catch(ClassCastException e){
        throw new ClassCastException(activity.toString() + " must implement OnRegComplete");
    }
}

public Dialog onCreateDialog(Bundle state){
    Dialog d = new Dialog(getActivity());
    d.requestWindowFeature(Window.FEATURE_NO_TITLE);
    d.setContentView(R.layout.registration);
    d.setCanceledOnTouchOutside(false);

    return d;
}

public interface OnRegComplete{
    void onRegComplete();
}

}

this is my layout

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/imageView1"
    android:layout_marginLeft="20dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:layout_marginRight="20dp"
    android:textSize="30sp"
    android:text="Registration" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView3"
    android:layout_marginLeft="26dp"
    android:layout_marginTop="16dp"
    android:text="State"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_below="@+id/imageView1"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_marginTop="10dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/reg_title" />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView4"
    android:layout_toRightOf="@+id/textView4"
    android:layout_marginRight="50dp"
    android:minWidth="300dp"
    android:layout_alignLeft="@+id/editText1"
     />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView4"
    android:layout_below="@+id/textView4"
    android:layout_marginTop="40dp"
    android:text="Station"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="40dp"
    android:text="County"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView4"
    android:layout_marginRight="50dp"
    android:minWidth="300dp"
    android:layout_alignLeft="@+id/editText1" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView6"
    android:layout_toRightOf="@+id/textView6"
    android:layout_marginRight="50dp"
    android:minWidth="300dp"
    android:layout_marginLeft="20dp"
    android:ems="10"
    android:inputType="textPassword" />

<Spinner
    android:id="@+id/spinner3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView6"
    android:layout_marginRight="50dp"
    android:minWidth="300dp"
    android:layout_alignLeft="@+id/editText1" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="36dp"
    android:text="@string/reg_btn" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView5"
    android:layout_below="@+id/textView5"
    android:layout_marginTop="42dp"
    android:text="Password"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="60dp"
    android:layout_marginTop="15dp"
    android:src="@drawable/mobile_mapr_logo" />

</RelativeLayout>

Answer

I had to give focus to a view above the edit text using

android:focusable="true"
android:focusableInTouchMode="true"

in the view

Categories
discuss

for loops in object within object

Can someone explain why doesn’t this work?

I have two objects within an object. I use for loops to print out each property within the nested objects, one after another.

var people = {
    john: {
        name: "John",
        age: 20
    },
    bob: {
        name: "Bob",
        age: 40
    }
};

for (var person in people) {
    for (var property in person) { 
        console.log(property);
    }
}

I expect it to print out:

name
age
name
age

Instead I get:

0
1
2
3
0
1
2

1) What am I doing wrong?

2) What exactly is the console doing to output the numbers above?

Answer

It is because in the second (nested) for loop you iterate string-valued person variables which hold property names (not values!) of people object. You should change it to people[person]:

for (var property in people[person]) { 
    console.log(property);
}

The numbers above correspond to indices of chars in string values:

0: j    0: b
1: o    1: o
2: h    2: b
3: n
Categories
discuss

Jersey: redirect outside of Jersey root

I’ve been writing a prototype Jersey (JAX-RS) application and wanted to try handling application/x-www-form-urlencoded posts with a redirect-after-POST methodology.

I want to redirect to an html page hosted at the application root on success, however I can’t seem to escape out of Jersey’s servlet root.

Here’s an example of a resource which allows you to create a new user:

URI I want: /jersey-test/user.html

URI I get: /jersey-test/r/user.html

@POST
@Consumes({MediaType.APPLICATION_FORM_URLENCODED})
public Response putUser(@Context UriInfo uriInfo, 
    MultivaluedMap<String, String> formParams) {

    // snip... do work and insert user here...

    URI uri = uriInfo.getBaseUriBuilder().path("user.html").build();
    return Response.seeOther(uri).build();
}

Relevant snippets from my web.xml:

<web-app ...>
  <display-name>jersey-test</display-name>
  ...
  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    ...
  </servlet>
  ...
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/r/*</url-pattern>
  </servlet-mapping>
</web-app>

Answer

Assign the path like this:

URI uri = uriInfo.getBaseUriBuilder().path("../user.html").build();
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..