Coder Social home page Coder Social logo

Comments (9)

MiguelAngelLV avatar MiguelAngelLV commented on June 27, 2024 1

@madlymad I used a normal Preference and set onPreferenceClick to launch Ringstone Select.

public boolean onPreferenceClick(Preference preference)
            SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
            String path = settings.getString("alarma", "content://settings/system/notification_sound");

            Uri uri = !TextUtils.isEmpty(path) ? Uri.parse(path) : null;


            Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);

            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, uri);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);

            startActivityForResult(intent, 0);
}

public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
         if (resultCode != Activity.RESULT_OK )
             return;


         Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
         SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
         Editor editor = settings.edit();
         editor.putString("alarma", uri == null ? "": uri.toString());
         editor.commit();             

}

from android-support-v4-preferencefragment.

tbfr avatar tbfr commented on June 27, 2024 1

The problem is related to this code in RingtonePreference:

PreferenceFragment owningFragment = getPreferenceManager().getFragment();
if (owningFragment != null) {
    owningFragment.startActivityForResult(intent, mRequestCode);
} else {
    getPreferenceManager().getActivity().startActivityForResult(intent, mRequestCode);
}

We have no owning fragment since PreferenceManager.setFragment() needs a PreferenceFragment and therefore the PreferenceManagerCompat can't set it.

But as you see in the above snippet, if no owner fragment is found the Activity is used instead to start ringtone picker. The workaround I use is to catch the result in my Activity like this:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (data == null) {
        return;
    }

    Bundle bundle = data.getExtras();

    if (bundle != null) {
        Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
        String ringtoneString = "";

        if (uri != null) {
            ringtoneString = uri.toString();
        }

        sharedPrefs.edit().putString("yourRingtoneKey", ringtoneString).commit();
    }
}

License

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to http://unlicense.org/

from android-support-v4-preferencefragment.

madlymad avatar madlymad commented on June 27, 2024

Any workaround/fix for that?

from android-support-v4-preferencefragment.

madlymad avatar madlymad commented on June 27, 2024

@MiguelAngelLV thanks that worked!


Tip of the problem that may help (for patching at the lib):

After searching android grep code related to RingtonePreference I found that the problem is that the onActivityResult is initialized at the Activity level that hosts your fragment so PreferenceFragment.class or your extension never get the activity result callback in order to save the selected ringtone.

I tried to fix that in library level but with no success.


Workaround (much easier for me):
In your code though you can patch the problem by passing the activity result from your Activity to your Fragment that extends PreferenceFragment.

  1. at your xml you can use RingtonePreference

  2. at your PreferencesActivity override:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        PreferenceActivityCallbacks callback = null;
        SherlockFragment f = this.getSupportFragmentManager().findFragmentByTag("your_fragment_tag");
        if ( f instanceof PreferenceActivityCallbacks )
            callback = (PreferenceActivityCallbacks)  f;
        if (callback != null) {
            callback.onActivityResult(requestCode, resultCode, data);
        }
    }
    
  3. PreferenceActivityCallbacks is my interface that I use to pass data from activity to fragment

    public interface PreferenceActivityCallbacks {
            // ...
            public void onActivityResult(int requestCode, int resultCode, Intent data);
            // ...
    }
    
  4. You are ready, you do not have to override onActivityResult in your fragment just implement the interface
    public class YourPreferenceFragment extends PreferenceFragment implements PreferenceActivityCallbacks {

from android-support-v4-preferencefragment.

agrajaghh avatar agrajaghh commented on June 27, 2024

hey @tbfr, thanks for your workaround! I would like to use it in an open source project (TextSecure). Could you please do us a favor and add a license (GPL, Apache?) to your code? I'm no lawyer but I would like to be on the save side ;)

from android-support-v4-preferencefragment.

tbfr avatar tbfr commented on June 27, 2024

Hey @agrajaghh!

Please feel free to use the code in any way you want :)

from android-support-v4-preferencefragment.

dominicoder avatar dominicoder commented on June 27, 2024

Just came across this same issue. Thanks @tbfr for the info. Looking into a bit further, you can simplify this by just delegating the onActivityResult call to the wrapped PreferenceFragment from your Activity. That, in turn, dispatches the call to the RingtonePreference that initiated the call to begin with.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    Fragment fragment = getSupportFragmentManager().findFragmentById(ID);
    fragment.onActivityResult(requestCode, resultCode, data);
}

Hope that's helpful.

from android-support-v4-preferencefragment.

alexstyl avatar alexstyl commented on June 27, 2024

Thε easiest and hassle free solution would be to do something like this on your Activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    List<Fragment> fragments = getSupportFragmentManager().getFragments();
    if (fragments != null) {
        for (Fragment frag : fragments)
            frag.onActivityResult(requestCode, resultCode, data);
    }
}

That way the activity is going to forward the onActivityResult to all hosted fragments, without having to keep their references or ids or tags by yourself

from android-support-v4-preferencefragment.

crysxd avatar crysxd commented on June 27, 2024

@alexstyl Thanks man, super simple solution and its working with multiple RingtonPreferences in one Activity.

from android-support-v4-preferencefragment.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.