সিঙ্ক অ্যাডাপ্টার ফ্রেমওয়ার্ক ধরে নেয় যে আপনার সিঙ্ক অ্যাডাপ্টার অ্যাকাউন্ট সম্পর্কিত ডিভাইস স্টোরেজ এবং লগ-ইন অ্যাক্সেস প্রয়োজন এমন সার্ভার স্টোরেজের মধ্যে ডেটা ট্রান্সফার করে। এই কারণে, ফ্রেমওয়ার্ক আশা করে যে আপনি সিঙ্ক অ্যাডাপ্টারের অংশ হিসেবে একটি কম্পোনেন্ট প্রদান করবেন, যাকে 'Authenticator' বলা হয়। এই কম্পোনেন্ট Android অ্যাকাউন্ট ও যাচাইকরণ ফ্রেমওয়ার্কের সাথে কানেক্ট করে এবং লগ-ইন তথ্যের মতো ব্যবহারকারীর ক্রেডেনশিয়াল ম্যানেজ করার জন্য একটি স্ট্যান্ডার্ড ইন্টারফেস প্রদান করে।
আপনার অ্যাপ অ্যাকাউন্ট ব্যবহার না করলেও, আপনাকে একটি যাচাইকারী কম্পোনেন্ট প্রদান করতে হবে।
আপনি অ্যাকাউন্ট বা সার্ভার লগ-ইন ব্যবহার না করলে, যাচাইকারী দ্বারা ম্যানেজ করা তথ্য
উপেক্ষা করা হয়, তাই আপনি যাচাইকারী কম্পোনেন্ট প্রদান করতে পারেন যাতে স্টাব পদ্ধতি
ইমপ্লিমেন্টেশন থাকে। এছাড়াও, আপনাকে একটি বাউন্ড Service প্রদান করতে হবে যা
সিঙ্ক অ্যাডাপ্টার ফ্রেমওয়ার্ককে যাচাইকারীর পদ্ধতি কল করার অনুমতি দেয়।
এই টিউটোরিয়ালে, সিঙ্ক অ্যাডাপ্টার ফ্রেমওয়ার্কের প্রয়োজনীয়তা পূরণ করার জন্য
স্টাব যাচাইকারীর সব অংশ কীভাবে নির্ধারণ করতে হয় তা দেখানো হয়েছে। ব্যবহারকারীর অ্যাকাউন্ট ম্যানেজ করে এমন একটি আসল
যাচাইকারী প্রদান করতে হলে, AbstractAccountAuthenticator-এর রেফারেন্স ডকুমেন্ট পড়ুন।
স্টাব যাচাইকারী কম্পোনেন্ট যোগ করা
আপনার অ্যাপে স্টাব অথেন্টিকেটর কম্পোনেন্ট যোগ করতে, এমন একটি ক্লাস তৈরি করুন যা
AbstractAccountAuthenticator-এর এক্সটেন্ড করে এবং তারপরে প্রয়োজনীয় পদ্ধতি স্টাব করুন,
null রিটার্ন করে অথবা ব্যতিক্রম ঘটানোর মাধ্যমে।
নিচের স্নিপেটে স্টাব অথেন্টিকেটর ক্লাসের একটি উদাহরণ দেখানো হয়েছে:
Kotlin
/* * Implement AbstractAccountAuthenticator and stub out all * of its methods */ class Authenticator(context: Context) // Simple constructor : AbstractAccountAuthenticator(context) { // Editing properties is not supported override fun editProperties(r: AccountAuthenticatorResponse, s: String): Bundle { throw UnsupportedOperationException() } // Don't add additional accounts @Throws(NetworkErrorException::class) override fun addAccount( r: AccountAuthenticatorResponse, s: String, s2: String, strings: Array<String>, bundle: Bundle ): Bundle? = null // Ignore attempts to confirm credentials @Throws(NetworkErrorException::class) override fun confirmCredentials( r: AccountAuthenticatorResponse, account: Account, bundle: Bundle ): Bundle? = null // Getting an authentication token is not supported @Throws(NetworkErrorException::class) override fun getAuthToken( r: AccountAuthenticatorResponse, account: Account, s: String, bundle: Bundle ): Bundle { throw UnsupportedOperationException() } // Getting a label for the auth token is not supported override fun getAuthTokenLabel(s: String): String { throw UnsupportedOperationException() } // Updating user credentials is not supported @Throws(NetworkErrorException::class) override fun updateCredentials( r: AccountAuthenticatorResponse, account: Account, s: String, bundle: Bundle ): Bundle { throw UnsupportedOperationException() } // Checking features for the account is not supported @Throws(NetworkErrorException::class) override fun hasFeatures( r: AccountAuthenticatorResponse, account: Account, strings: Array<String> ): Bundle { throw UnsupportedOperationException() } }
জাভা
/* * Implement AbstractAccountAuthenticator and stub out all * of its methods */ public class Authenticator extends AbstractAccountAuthenticator { // Simple constructor public Authenticator(Context context) { super(context); } // Editing properties is not supported @Override public Bundle editProperties( AccountAuthenticatorResponse r, String s) { throw new UnsupportedOperationException(); } // Don't add additional accounts @Override public Bundle addAccount( AccountAuthenticatorResponse r, String s, String s2, String[] strings, Bundle bundle) throws NetworkErrorException { return null; } // Ignore attempts to confirm credentials @Override public Bundle confirmCredentials( AccountAuthenticatorResponse r, Account account, Bundle bundle) throws NetworkErrorException { return null; } // Getting an authentication token is not supported @Override public Bundle getAuthToken( AccountAuthenticatorResponse r, Account account, String s, Bundle bundle) throws NetworkErrorException { throw new UnsupportedOperationException(); } // Getting a label for the auth token is not supported @Override public String getAuthTokenLabel(String s) { throw new UnsupportedOperationException(); } // Updating user credentials is not supported @Override public Bundle updateCredentials( AccountAuthenticatorResponse r, Account account, String s, Bundle bundle) throws NetworkErrorException { throw new UnsupportedOperationException(); } // Checking features for the account is not supported @Override public Bundle hasFeatures( AccountAuthenticatorResponse r, Account account, String[] strings) throws NetworkErrorException { throw new UnsupportedOperationException(); } }
ফ্রেমওয়ার্কের সাথে যাচাইকারীকে বাইন্ড করা
সিঙ্ক অ্যাডাপ্টার ফ্রেমওয়ার্ককে আপনার যাচাইকারী অ্যাক্সেস করতে দেওয়ার জন্য, আপনাকে এর জন্য একটি বাউন্ড পরিষেবা তৈরি করতে হবে। এই পরিষেবা একটি Android বাইন্ডার অবজেক্ট প্রদান করে যা ফ্রেমওয়ার্ককে আপনার যাচাইকারীকে কল করতে এবং যাচাইকারী ও ফ্রেমওয়ার্কের মধ্যে ডেটা পাস করতে দেয়।
নিচে দেওয়া স্নিপেট থেকে কীভাবে বাউন্ড Service নির্ধারণ করতে হয় তা জানতে পারবেন:
Kotlin
/** * A bound Service that instantiates the authenticator * when started. */ class AuthenticatorService : Service() { // Instance field that stores the authenticator object private lateinit var mAuthenticator: Authenticator override fun onCreate() { // Create a new authenticator object mAuthenticator = Authenticator(getApplicationContext()) } /* * When the system binds to this Service to make the RPC call * return the authenticator's IBinder. */ override fun onBind(intent: Intent?): IBinder = mAuthenticator.iBinder }
জাভা
/** * A bound Service that instantiates the authenticator * when started. */ public class AuthenticatorService extends Service { ... // Instance field that stores the authenticator object private Authenticator mAuthenticator; @Override public void onCreate() { // Create a new authenticator object mAuthenticator = new Authenticator(getApplicationContext()); } /* * When the system binds to this Service to make the RPC call * return the authenticator's IBinder. */ @Override public IBinder onBind(Intent intent) { return mAuthenticator.getIBinder(); } }
যাচাইকারী মেটাডেটা ফাইল যোগ করুন
সিঙ্ক অ্যাডাপ্টার ও অ্যাকাউন্ট ফ্রেমওয়ার্কে আপনার যাচাইকারী কম্পোনেন্ট প্লাগ-ইন করতে, আপনাকে
এইসব ফ্রেমওয়ার্ককে কম্পোনেন্ট বর্ণনা করে এমন মেটাডেটা দিতে হবে। এই মেটাডেটা আপনার সিঙ্ক অ্যাডাপ্টারের জন্য তৈরি করা
অ্যাকাউন্টের ধরন ঘোষণা করে এবং আপনি যদি ব্যবহারকারীকে নিজের অ্যাকাউন্টের ধরন দেখাতে চান, তাহলে সিস্টেমের দেখানো
ইউজার ইন্টারফেস এলিমেন্ট ঘোষণা করে। আপনার অ্যাপ প্রোজেক্টের /res/xml/ ডিরেক্টরিতে সেভ করা XML ফাইলে এই
মেটাডেটা ঘোষণা করুন।
আপনি ফাইলটির যেকোনও নাম দিতে পারেন, যদিও এটি সাধারণত authenticator.xml নামে পরিচিত।
এই XML ফাইলে একটি এলিমেন্ট <account-authenticator> আছে, যার
নিম্নলিখিত অ্যাট্রিবিউট আছে:
-
android:accountType -
সিঙ্ক অ্যাডাপ্টার ফ্রেমওয়ার্কের জন্য প্রতিটি সিঙ্ক অ্যাডাপ্টারের একটি অ্যাকাউন্ট ধরন থাকতে হবে, যা
ডোমেন নামের আকারে থাকে। ফ্রেমওয়ার্ক, সিঙ্ক অ্যাডাপ্টরের
ইন্টার্নাল শনাক্তকরণের অংশ হিসেবে অ্যাকাউন্টের ধরন ব্যবহার করে। লগ-ইন করার প্রয়োজন হয় এমন সার্ভারের ক্ষেত্রে, লগ-ইন ক্রেডেনশিয়ালের অংশ হিসেবে
ব্যবহারকারীর অ্যাকাউন্টের সাথে অ্যাকাউন্টের ধরন সার্ভারে পাঠানো হয়।
আপনার সার্ভারে লগ-ইন করার প্রয়োজন না থাকলেও, আপনাকে অ্যাকাউন্টের ধরন দিতে হবে। ভ্যালুর জন্য, আপনার নিয়ন্ত্রণাধীন ডোমেনের নাম ব্যবহার করুন। ফ্রেমওয়ার্ক আপনার সিঙ্ক অ্যাডাপ্টার ম্যানেজ করার জন্য এটি ব্যবহার করলেও, ভ্যালু আপনার সার্ভারে পাঠানো হয় না।
-
android:icon -
আইকন থাকা Drawable
রিসোর্সের পয়েন্টার। আপনি যদি
অ্যাট্রিবিউট নির্দিষ্ট করে সিঙ্ক অ্যাডাপ্টারকে দৃশ্যমান করেন
android:userVisible="true"res/xml/syncadapter.xml, তাহলে আপনাকে অবশ্যই এই আইকন রিসোর্স প্রদান করতে হবে। এটি সিস্টেমের সেটিংস অ্যাপের অ্যাকাউন্ট বিভাগে দেখা যায়। -
android:smallIcon -
আইকনের ছোট ভার্সন সহ Drawable
রিসোর্সের পয়েন্টার। স্ক্রিনের সাইজের উপর নির্ভর করে, সিস্টেমের সেটিংস অ্যাপের অ্যাকাউন্ট বিভাগে
android:icon-এর পরিবর্তে এই রিসোর্স ব্যবহার করা হতে পারে। -
android:label -
লোকালাইজ করা যায় এমন স্ট্রিং যা ব্যবহারকারীদের অ্যাকাউন্টের ধরন শনাক্ত করে। আপনি যদি সিঙ্ক অ্যাডাপ্টারকে
android:userVisible="true"-এ অ্যাট্রিবিউট নির্দিষ্ট করেres/xml/syncadapter.xmlদৃশ্যমান করেন, তাহলে আপনাকে এই স্ট্রিং প্রদান করতে হবে। এটি সিস্টেমের সেটিংস অ্যাপের অ্যাকাউন্ট বিভাগে, আপনার ডিফাইন করা Authenticator-এর আইকনের পাশে দেখা যায়।
নিচের স্নিপেটে আপনার আগে তৈরি করা যাচাইকারীর XML ফাইল দেখানো হয়েছে:
<?xml version="1.0" encodin>g<="utf-8"? account-authenticator xmlns:android="http://schemas.android.com/apk/res/android" android:accountType="@example.com" android:icon="draw@able/ic_launcher" android:smallI@con="drawabl>e/ic_launcher" android:label="string/app_name"/
ম্যানিফেস্টে যাচাইকারী ঘোষণা করা
আগের ধাপে, আপনি একটি বাউন্ড Service তৈরি করেছেন যা সিঙ্ক অ্যাডাপ্টার ফ্রেমওয়ার্কের সাথে
যাচাইকারীকে লিঙ্ক করে। সিস্টেমের কাছে এই পরিষেবা শনাক্ত করতে, আপনার অ্যাপের
ম্যানিফেস্টে নিম্নলিখিত
<service>
এলিমেন্টকে
<application>-এর চাইল্ড এলিমেন্ট হিসেবে যোগ করে ঘোষণা করুন:
<service android:name="com.example.android.syncadapter.AuthenticatorSer>vice"<; int>ent-filter < action android:name="android.accounts.Account>Authentic<ator"/ > /in<tent-filter meta-data android:name="android.accounts.AccountAuthenticator" android:r>esour<ce=">;@xml/authenticator" / /service
<intent-filter>
এলিমেন্ট এমন একটি ফিল্টার সেট-আপ করে যা ইনটেন্ট অ্যাকশন
android.accounts.AccountAuthenticator দ্বারা ট্রিগার করা হয়, যা সিস্টেমের মাধ্যমে
Authenticator চালানোর জন্য পাঠানো হয়। ফিল্টার ট্রিগার করা হলে, সিস্টেম AuthenticatorService,
আপনার দেওয়া বাউন্ড Service শুরু করে যা দিয়ে যাচাইকারীকে মোড়ানো হয়।
The
<meta-data>
এলিমেন্ট যাচাইকারীর মেটাডেটা ঘোষণা করে। The
android:name
অ্যাট্রিবিউট, যাচাইকরণ ফ্রেমওয়ার্কের সাথে মেটা-ডেটা লিঙ্ক করে। The
android:resource
এলিমেন্টটি আপনার আগে তৈরি করা যাচাইকারী মেটাডেটা ফাইলের নাম নির্দিষ্ট করে।
যাচাইকারী ছাড়াও, সিঙ্ক অ্যাডাপ্টরের কন্টেন্ট প্রদানকারী প্রয়োজন। আপনার অ্যাপ আগে থেকেই কোনও কন্টেন্ট প্রদানকারী ব্যবহার না করলে, কীভাবে স্টাব কন্টেন্ট প্রদানকারী তৈরি করতে হয় তা জানতে পরবর্তী লেসনে যান; অন্যথায়, সিঙ্ক অ্যাডাপ্টর তৈরি করা লেসনে যান।