Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Static PreferenceManager.GetDefaultSharedPreferences missing. #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
YevgenijZ opened this issue Jan 9, 2020 · 27 comments
Closed

Static PreferenceManager.GetDefaultSharedPreferences missing. #41

YevgenijZ opened this issue Jan 9, 2020 · 27 comments
Assignees
Labels
in-progress in progress

Comments

@YevgenijZ
Copy link

Xamarin.Android Version (eg: 6.0):

Microsoft Visual Studio Community 2019
Version 16.4.2
Microsoft .NET Framework
Version 4.8.03752
Xamarin.Android SDK 10.1.1.0 (d16-4/f2c9364)

Operating System & Version (eg: Mac OSX 10.11):

Windows 10 (1909)

Support Libraries Version (eg: 23.3.0):

Xamarin.AndroidX.Preference 1.1.0-rc1

Describe your Issue:

PreferenceManager.GetDefaultSharedPreferences(this.context)
Error CS0117 'PreferenceManager' does not contain a definition for 'GetDefaultSharedPreferences'

The only static member available on PreferenceManager is SetDefaultValues (and the ones from base Object).

@auedbaki
Copy link

auedbaki commented Jan 9, 2020

Getting same issue Xamarin.AndroidX.Preference 1.1.0-rc1
and No FindPreference Method on Xamarin.AndroidX.Preference 1.1.0-preview6

@moljac moljac self-assigned this Jan 12, 2020
@moljac
Copy link
Contributor

moljac commented Jan 20, 2020

@YevgenijZ
Well I did not change anything in the bindings. Only updates (OS, IDE, tooling)...

in my generated code:

		static Delegate cb_getSharedPreferences;
#pragma warning disable 0169
		static Delegate GetGetSharedPreferencesHandler ()
		{
			if (cb_getSharedPreferences == null)
				cb_getSharedPreferences = JNINativeWrapper.CreateDelegate ((Func<IntPtr, IntPtr, IntPtr>) n_GetSharedPreferences);
			return cb_getSharedPreferences;
		}

		static IntPtr n_GetSharedPreferences (IntPtr jnienv, IntPtr native__this)
		{
			global::AndroidX.Preference.PreferenceManager __this = global::Java.Lang.Object.GetObject<global::AndroidX.Preference.PreferenceManager> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
			return JNIEnv.ToLocalJniHandle (__this.SharedPreferences);
		}
#pragma warning restore 0169

		public virtual unsafe global::Android.Content.ISharedPreferences SharedPreferences {
			// Metadata.xml XPath method reference: path="/api/package[@name='androidx.preference']/class[@name='PreferenceManager']/method[@name='getSharedPreferences' and count(parameter)=0]"
			[Register ("getSharedPreferences", "()Landroid/content/SharedPreferences;", "GetGetSharedPreferencesHandler")]
			get {
				const string __id = "getSharedPreferences.()Landroid/content/SharedPreferences;";
				try {
					var __rm = _members.InstanceMethods.InvokeVirtualObjectMethod (__id, this, null);
					return global::Java.Lang.Object.GetObject<global::Android.Content.ISharedPreferences> (__rm.Handle, JniHandleOwnership.TransferLocalRef);
				} finally {
				}
			}
		}

So getSharedPreferences is bound as property.

@YevgenijZ
Copy link
Author

@moljac
Although I mentioned GetDefaultSharedPreferences(Context context) - Java: getDefaultSharedPreferences(Context context)

@moljac
Copy link
Contributor

moljac commented Jan 20, 2020

@YevgenijZ

This

		// Metadata.xml XPath method reference: path="/api/package[@name='androidx.preference']/class[@name='PreferenceManager']/method[@name='getDefaultSharedPreferences' and count(parameter)=1 and parameter[1][@type='android.content.Context']]"
		[Register ("getDefaultSharedPreferences", "(Landroid/content/Context;)Landroid/content/SharedPreferences;", "")]
		public static unsafe global::Android.Content.ISharedPreferences GetDefaultSharedPreferences (global::Android.Content.Context context)
		{
			const string __id = "getDefaultSharedPreferences.(Landroid/content/Context;)Landroid/content/SharedPreferences;";
			try {
				JniArgumentValue* __args = stackalloc JniArgumentValue [1];
				__args [0] = new JniArgumentValue ((context == null) ? IntPtr.Zero : ((global::Java.Lang.Object) context).Handle);
				var __rm = _members.StaticMethods.InvokeObjectMethod (__id, __args);
				return global::Java.Lang.Object.GetObject<global::Android.Content.ISharedPreferences> (__rm.Handle, JniHandleOwnership.TransferLocalRef);
			} finally {
			}
		}

??

@moljac
Copy link
Contributor

moljac commented Jan 20, 2020

@auedbaki
In downloaded decompiled package (ILSpy) I see FindPreference 2 overloads.

https://www.nuget.org/packages/Xamarin.AndroidX.Preference/1.1.0-rc1

Generated code:

		static Delegate cb_findPreference_Ljava_lang_CharSequence_;
#pragma warning disable 0169
		static Delegate GetFindPreference_Ljava_lang_CharSequence_Handler ()
		{
			if (cb_findPreference_Ljava_lang_CharSequence_ == null)
				cb_findPreference_Ljava_lang_CharSequence_ = JNINativeWrapper.CreateDelegate ((Func<IntPtr, IntPtr, IntPtr, IntPtr>) n_FindPreference_Ljava_lang_CharSequence_);
			return cb_findPreference_Ljava_lang_CharSequence_;
		}

		static IntPtr n_FindPreference_Ljava_lang_CharSequence_ (IntPtr jnienv, IntPtr native__this, IntPtr native_key)
		{
			global::AndroidX.Preference.PreferenceManager __this = global::Java.Lang.Object.GetObject<global::AndroidX.Preference.PreferenceManager> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
			global::Java.Lang.ICharSequence key = global::Java.Lang.Object.GetObject<global::Java.Lang.ICharSequence> (native_key, JniHandleOwnership.DoNotTransfer);
			IntPtr __ret = JNIEnv.ToLocalJniHandle (__this.FindPreference (key));
			return __ret;
		}
#pragma warning restore 0169

		// Metadata.xml XPath method reference: path="/api/package[@name='androidx.preference']/class[@name='PreferenceManager']/method[@name='findPreference' and count(parameter)=1 and parameter[1][@type='java.lang.CharSequence']]"
		[Register ("findPreference", "(Ljava/lang/CharSequence;)Landroidx/preference/Preference;", "GetFindPreference_Ljava_lang_CharSequence_Handler")]
		[global::Java.Interop.JavaTypeParameters (new string [] {"T extends androidx.preference.Preference"})]
		public virtual unsafe global::Java.Lang.Object FindPreference (global::Java.Lang.ICharSequence key)
		{
			const string __id = "findPreference.(Ljava/lang/CharSequence;)Landroidx/preference/Preference;";
			IntPtr native_key = CharSequence.ToLocalJniHandle (key);
			try {
				JniArgumentValue* __args = stackalloc JniArgumentValue [1];
				__args [0] = new JniArgumentValue (native_key);
				var __rm = _members.InstanceMethods.InvokeVirtualObjectMethod (__id, this, __args);
				return (Java.Lang.Object) global::Java.Lang.Object.GetObject<global::Java.Lang.Object> (__rm.Handle, JniHandleOwnership.TransferLocalRef);
			} finally {
				JNIEnv.DeleteLocalRef (native_key);
			}
		}

		public global::Java.Lang.Object FindPreference (string key)
		{
			global::Java.Lang.String jls_key = key == null ? null : new global::Java.Lang.String (key);
			global::Java.Lang.Object __result = FindPreference (jls_key);
			var __rsval = __result;
			jls_key?.Dispose ();
			return __rsval;
		}

@YevgenijZ
Copy link
Author

YevgenijZ commented Jan 20, 2020

I'm using Androidx.Preferences from NuGet - pre version "-rc1" and GetDefaultSharedPreferences is missing, dotPeek does not show presence of this method either, method was available in pre version "-preview06". Maybe something wrong was uploaded to NuGet?

@moljac
Copy link
Contributor

moljac commented Jan 20, 2020

@YevgenijZ @auedbaki

We work on our bindings tools too, so that might be the reason. I don't think that nuget upload/publishing would mess something up.

I will close this issue. Methods/properties should appear in rc2 or stable soon.

@auedbaki
Copy link

@moljac
On writing
ISharedPreferences sharedPreferences = PreferenceManager.GetDefaultSharedPreferences(Application.Context);
Getting Error:
Error CS1061: 'PreferenceManager' does not contain a definition for 'GetDefaultSharedPreferences' and no accessible extension method 'GetDefaultSharedPreferences' accepting a first argument of type 'PreferenceManager' could be found (are you missing a using directive or an assembly reference?)

On register and unregister using this code:

public override void OnResume()
        {
            base.OnResume();
            PreferenceScreen.SharedPreferences.RegisterOnSharedPreferenceChangeListener(this);
        }

        public override void OnPause()
        {
            base.OnPause();
            PreferenceScreen.SharedPreferences.UnregisterOnSharedPreferenceChangeListener(this);
        }

Getting error about SharedPreferences from PreferenceScreen
Error CS1061: 'PreferenceScreen' does not contain a definition for 'SharedPreferences' and no accessible extension method 'SharedPreferences' accepting a first argument of type 'PreferenceScreen' could be found (are you missing a using directive or an assembly reference?) (CS1061)

@YevgenijZ
Copy link
Author

@moljac
Why not delist rc1 package it's missing crucial part of androidx.preference api?

@moljac
Copy link
Contributor

moljac commented Jan 20, 2020

@YevgenijZ I cannot delist packages. What else is missing?

@moljac
Copy link
Contributor

moljac commented Jan 20, 2020

@auedbaki
I am aware of GetDefaultSharedPreferences missing.

Regarding PreferenceScreen it would make sense to open new issue.

@YevgenijZ
Copy link
Author

Alright then. Makes sense to close this issue when fix will be confirmed though to have public information that rc1 is missing something. Anyway i'm happy that you see the problem and it will be fixed.

@moljac
Copy link
Contributor

moljac commented Jan 20, 2020

@auedbaki

Decompiled java:

package androidx.preference;

import android.content.*;
import android.util.*;
import androidx.core.content.res.*;

public final class PreferenceScreen extends PreferenceGroup
{
    private boolean mShouldUseGeneratedIds;
    
    public PreferenceScreen(final Context context, final AttributeSet attrs) {
        super(context, attrs, TypedArrayUtils.getAttr(context, R.attr.preferenceScreenStyle, 16842891));
        this.mShouldUseGeneratedIds = true;
    }
    
    @Override
    protected void onClick() {
        if (this.getIntent() != null || this.getFragment() != null || this.getPreferenceCount() == 0) {
            return;
        }
        final PreferenceManager.OnNavigateToScreenListener listener = this.getPreferenceManager().getOnNavigateToScreenListener();
        if (listener != null) {
            listener.onNavigateToScreen(this);
        }
    }
    
    @Override
    protected boolean isOnSameScreenAsChildren() {
        return false;
    }
    
    public boolean shouldUseGeneratedIds() {
        return this.mShouldUseGeneratedIds;
    }
    
    public void setShouldUseGeneratedIds(final boolean shouldUseGeneratedIds) {
        if (this.isAttached()) {
            throw new IllegalStateException("Cannot change the usage of generated IDs while attached to the preference hierarchy");
        }
        this.mShouldUseGeneratedIds = shouldUseGeneratedIds;
    }
}

@auedbaki
Copy link

@moljac You are right this is not the issue with PreferenceScreen, I just check the Original Docs from Google, and can say PreferenceScreen Class is Clean, and has no bug. May be it can be the issue of Binding Tools too.

@YevgenijZ
Copy link
Author

YevgenijZ commented Jan 20, 2020

PreferenceScreen is derived from PreferenceGroup and therefore Preference and Preference should contain getSharedPreferences (SharedPreference in C#).
The code PreferenceScreen.SharedPreferences.RegisterOnSharedPreferenceChangeListener assuming it's inside PreferenceFragment should compile without CS1061.

@moljac
Copy link
Contributor

moljac commented Jan 21, 2020

@YevgenijZ @auedbaki

Can you check rc1 of the package please?

NOTE: seems that the class is still missing. Not sure. There is duplicate issue with the details. Method is generated locally, but seems not to be generated on the CI servers during build and publish. This points out on the different tooling versions.

@moljac
Copy link
Contributor

moljac commented Jan 21, 2020

Duplicate
#46

@YevgenijZ
Copy link
Author

@moljac Did you mean check rc2? Yes, rc2 has same problem, method is missing.

@moljac
Copy link
Contributor

moljac commented Jan 22, 2020

Related:

dotnet/java-interop#501

@moljac
Copy link
Contributor

moljac commented Jan 22, 2020

Related:
dotnet/java-interop#543

CI servers should use 16.5 P2

@moljac
Copy link
Contributor

moljac commented Jan 23, 2020

@moljac
Copy link
Contributor

moljac commented Feb 3, 2020

@YevgenijZ @auedbaki

Both MacOSX and Windows Azure DevOps (CI) build servers are generating correct code now.

Fixed in:

https://github.com/xamarin/AndroidX/releases/tag/AndroidX-rc3

Waiting for Americans to wake up, so somebody will press the green button to publish nuget[s].

Please provide feedback whether the issue is fixed. Even better close it yourself. Thanks

@moljac
Copy link
Contributor

moljac commented Feb 3, 2020

@YevgenijZ @auedbaki

https://www.nuget.org/packages/Xamarin.AndroidX.Preference/1.1.0-rc3

Feedback please

@YevgenijZ
Copy link
Author

GetDefaultSharedPreferences is back in rc3. It's alright to close issue. Thanks!

@auedbaki
Copy link

auedbaki commented Feb 3, 2020

Yes, It's working now. You can close this issue.

@moljac
Copy link
Contributor

moljac commented Feb 3, 2020

Will leave it for a bit to get more info.

@auedbaki Thanks

@moljac
Copy link
Contributor

moljac commented Feb 3, 2020

Closing it. Preparing stable

@moljac moljac closed this as completed Feb 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in-progress in progress
Projects
None yet
Development

No branches or pull requests

3 participants