Integrating Twitter Authenticating using Twitter4j in Phimpme Android Application

We have used Twitter4j API to authenticate Twitter in Phimpme application. Below are the following steps in setting up the Twitter4j API in Phimpme and Login to Twitter from Phimpme android application. Setting up the environment Download the Twitter4j package from http://twitter4j.org/en/. For sharing images we will only need twitter4j-core-3.0.5.jar and twitter4j-media-support-3.0.5.jar files. Copy these files and save it in the libs folder of the application. Go to build.gradle and add the following codes in dependencies: dependencies { compile files('libs/twitter4j-core-3.0.5.jar') compile files('libs/twitter4j-media-support-3.0.5.jar') } Adding Phimpme application in Twitter development page Go to https://dev.twitter.com/->My apps-> Create new apps. Create an application window opens where we have to fill all the necessary details about the application. It is mandatory to fill all the fields. In website field, if you are making an android application then anything can be filled in website field for example www.google.com. But it is necessary to fill this field also. After filling all the details click on “Create your Twitter application” button. Adding Twitter Consumer Key and Secret Key This generates twitter consumer key and twitter secret key. We need to add this in our string.xml folder. <string name="twitter_consumer_key">ry1PDPXM6rwFVC1KhQ585bJPy</string> <string name="twitter_consumer_secret">O3qUqqBLinr8qrRvx3GXHWBB1AN10Ax26vXZdNlYlEBF3vzPFt</string> Twitter Authentication Make a new JAVA class say LoginActivity. Where we have to first fetch the twitter consumer key and Twitter secret key. private static Twitter twitter;    private static RequestToken requestToken;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_twitter_login);        twitterConsumerKey = getResources().getString(R.string.twitter_consumer_key);        twitterConsumerSecret = getResources().getString(R.string.twitter_consumer_secret);   We are using a web view to interact with the Twitter login page. twitterLoginWebView = (WebView)findViewById(R.id.twitterLoginWebView);        twitterLoginWebView.setBackgroundColor(Color.TRANSPARENT);        twitterLoginWebView.setWebViewClient( new WebViewClient(){            @Override            public boolean shouldOverrideUrlLoading(WebView view, String url){                if( url.contains(AppConstant.TWITTER_CALLBACK_URL)){                    Uri uri = Uri.parse(url);                    LoginActivity.this.saveAccessTokenAndFinish(uri);                    return true;                }                return false;            }              If the access Token is already saved then the user is already signed in or else it sends the Twitter consumer key and the Twitter secret key to gain access Token. ConfigurationBuilder function is used to set the consumer key and consumer secret key. ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();        configurationBuilder.setOAuthConsumerKey(twitterConsumerKey);     configurationBuilder.setOAuthConsumerSecret(twitterConsumerSecret);        Configuration configuration = configurationBuilder.build();        twitter = new TwitterFactory(configuration).getInstance(); It is followed by the following Runnable thread to check if the request token is received or not. If authentication fails, an error Toast message pops. new Thread(new Runnable() {            @Override            public void run() {                try {                    requestToken = twitter.getOAuthRequestToken(AppConstant.TWITTER_CALLBACK_URL);                } catch (Exception e) {                    final String errorString = e.toString();                    LoginActivity.this.runOnUiThread(new Runnable() {                        @Override                        public void run() {                            mAlertBuilder.cancel();                            Toast.makeText(LoginActivity.this, errorString, Toast.LENGTH_SHORT).show();                            finish();                        }                    });                    return;                }                LoginActivity.this.runOnUiThread(new Runnable() {                    @Override                    public void run() {                        twitterLoginWebView.loadUrl(requestToken.getAuthenticationURL());                    }                });            }        }).start(); Conclusion It offers seamless integration of Twitter in any application. Without leaving actual application, easier to authenticate. Further, it is used to upload the photo to Twitter directly from Phimpme Android application, fetch profile picture and username. Github https://github.com/fossasia/phimpme-android Resources To create app https://dev.twitter.com/ To download the Twitter4j Package: http://twitter4j.org/en/ Youtube tutorial: https://www.youtube.com/watch?v=_IsBi3cpvio Blog Post: http://www.theappguruz.com/blog/android-twitter-integration-tutorial  

Continue ReadingIntegrating Twitter Authenticating using Twitter4j in Phimpme Android Application

Controlling Camera Actions Using Voice Interaction in Phimpme Android

In this blog, I will explain how I implemented Google voice actions to control camera features on the Phimpme Android project. I will cover the following features I have implemented on the Phimpme project: Opening the application using Google Voice command. Switching between the cameras. Clicking a Picture and saving it through voice command. Opening application when the user gives a command to Google Now.                       When the user gives command “Take a selfie” or “Click a picture” to Google Now it directly opens Phimpme camera activity.  First                                                                                                                                        We need to add an intent filter to the manifest file so that Google Now can  detect Phimpme camera activity <activity   android:name=".opencamera.Camera.CameraActivity"   android:screenOrientation="portrait"   android:theme="@style/Theme.AppCompat.NoActionBar">   <intent-filter>       <action android:name="android.media.action.IMAGE_CAPTURE"/>       <category android:name="android.intent.category.DEFAULT"/>       <category android:name="android.intent.category.VOICE"/>   </intent-filter> </activity> category android:name=”android.intent.category.VOICE” is added to the IMAGE_CAPTURE intent filter for the Google Now to detect the camera activity. For the Google Now assistance to accept the command in the camera activity we need to add the following in the STILL_IMAGE_CAMERA intent filter in the camera activity. <intent-filter>   <action android:name="android.media.action.STILL_IMAGE_CAMERA"/>   <category android:name="android.intent.category.DEFAULT"/>   <category android:name="android.intent.category.VOICE"/> </intent-filter> So, now when the user says “OK Google” and “Take a Picture” the camera activity on Phimpme opens. Integrating Google Voice assistance in Camera Activity Second,                                                                                                                               After opening the camera application the Google Assistance should ask a question. The cameraActivity in Phimpme can be opened in two ways, such as: When opened from a different application. When given the command to Goole Now assistance. We need to check whether the camera activity is prompted from Google assistance or not to activate voice command. We will check it in onResume function. @Override public void onResume() { if (CameraActivity.this.isVoiceInteraction()) {     startVoiceTrigger();  } } If is.VoiceInteraction gives “true” then voice Assistance prompts.             Assistance to ask which camera to use Third,                                                                                                                              …

Continue ReadingControlling Camera Actions Using Voice Interaction in Phimpme Android

Share Images on Pinterest from Phimpme Android Application

After successfully establishing Pinterest authentication in Phimpme our next goal was to share the image on the Pinterest website directly from Phimpme, without using any native Android application. Adding Pinterest Sharing option in Sharing Activity in Phimpme To add various sharing options in Sharing Activity in the Phimpme project, I have applied a ScrollView for the list of the different sharing options which include: Facebook, Twitter, Pinterest, Imgur, Flickr and Instagram. All the App icons with the name are arranged in a TableLayout in the activity_share.xml file. Table rows consist of two columns. In this way, it is easier to add more app icons for future development. <ScrollView   android:layout_width="wrap_content"   android:layout_height="@dimen/scroll_view_height"   android:layout_above="@+id/share_done"   android:id="@+id/bottom_view">   <LinearLayout       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:orientation="vertical">       <TableLayout Adding Pinterest app icon on the icons_drawable array. This array is then used to inflate the icon on the list view. private int[] icons_drawables = {R.drawable.ic_facebook_black, R.drawable.ic_twitter_black,R.drawable.ic_instagram_black, R.drawable.ic_wordpress_black, R.drawable.ic_pinterest_black); Adding Pinterest text on the titles_text array. This array is then used to inflate the names of the various sharing activity. private int[] titles_text = {R.string.facebook, R.string.twitter, R.string.instagram,       R.string.wordpress, R.string.pinterest); Prerequisites to share Image on Pinterest To share an Image on Pinterest a user has to add a caption and Board ID. Our first milestone was to get the input of the Board ID  by the user. I have achieved this by taking the input in a Dialog Box. When the user clicks on the Pinterest option, a dialog box pops and then the user can add their Board ID. private void openPinterestDialogBox() {   AlertDialog.Builder captionDialogBuilder = new AlertDialog.Builder(SharingActivity.this, getDialogStyle());   final EditText captionEditText = getCaptionDialog(this, captionDialogBuilder);   captionEditText.setHint(R.string.hint_boardID);   captionDialogBuilder.setNegativeButton(getString(R.string.cancel).toUpperCase(), null);   captionDialogBuilder.setPositiveButton(getString(R.string.post_action).toUpperCase(), new DialogInterface.OnClickListener() {       @Override       public void onClick(DialogInterface dialog, int which) {           //This should be empty it will be overwrite later           //to avoid dismiss of the dialog on the wrong password       }   });   final AlertDialog passwordDialog = captionDialogBuilder.create();   passwordDialog.show();   passwordDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {       @Override       public void onClick(View v) {           String captionText = captionEditText.getText().toString();           boardID =captionText;           shareToPinterest(boardID);           passwordDialog.dismiss();       }   }); } A user can fetch the Board ID by following the steps: Copy the Board URL from the Pinterest main page. Paste the URL in the dialog box of the website: https://www.nutt.net/how-do-i-get-pinterest-board-id/ Copy the Board ID from the website and then past it in the AlertDialog Box. Board ID is necessary because it specifies where the image needs to be posted. Creating custom post function for Phimpme The image is posted using a function in PDKClient class. PDKClient is found in the PDK module which we get after importing Pinterest SDK. Every image is posted on Pinterest is called a Pin. So we will call createPin function. I have made my custom createPin function so that it also accepts Bitmaps as a parameter. In the Pinterest SDK it only accepts image URL to share, The image should already be on the internet to be shared. For this reason, we to add a custom create Pin function to accept Bitmaps as an option. public void createPin(String note, String boardId, Bitmap image, String link, PDKCallback callback) {   if (Utils.isEmpty(note)…

Continue ReadingShare Images on Pinterest from Phimpme Android Application

Integration of Flickr in Phimpme Android

Flickr is very widely used photo sharing website. So, we added a functionality in Phimpme Android to share the image to Flickr. Using this feature, the user can upload a picture to Flickr, without leaving the application with a single click. The way how we implemented this Flickr in our Phimpme Android application is shown below. Setting up Flickr API Flickr provides only an official web API. So we used the flickrj-android library built using the official Flickr API. First of all, we created an application in Flickr App Garden and requested API tokens from here. We noted down the resultant API key and secret key. These keys are necessary for authenticating Flickr in the application. Steps for Authentication Establishing the authentication with Flickr requires the following steps as per the official documentation. Getting a request token Getting User Authorization Exchanging the Request Token for an Access Token Use the Access Token with up.flickr.com/services/upload/ to upload pictures. Authorizing the user of Flickr in Phimpme As shown in the above picture, a request token has to be generated. Flickrj SDK provides it for us using Flickr object which further is created using the API key and Secret key. This request token is used for directing the user to a page for manually authorizing the Phimpme application to share the picture to his/her Flickr account. In Phimpme Android application, we created the Flickr object as shown below. Flickr f = new Flickr(API_KEY, API_SEC, new REST()); OAuthToken oauthToken = f.getOAuthInterface().getRequestToken(     OAUTH_CALLBACK_URI.toString()); saveTokenSecrent(oauthToken.getOauthTokenSecret()); URL oauthUrl = f.getOAuthInterface().buildAuthenticationUrl(     Permission.WRITE, oauthToken); Getting Access Token After the manual authorization of the application, Flickr returns the OAuth token and OAuth verifier. These have to used for making another request to Flickr for getting the “Access Token” which gives us the access to upload the picture to Flickr account. For implementing this, intent data is collected when the application reopens after manual authorization. This data contains the OAuth token and OAuth verifier. These are passed to get Access Token from functions present Flickrj library. In Phimpme Android, we implemented this in following way. Uri uri = intent.getData(); String query = uri.getQuery(); String[] data = query.split("&"); if (data != null && data.length == 2) {  String oauthToken = data[0].substring(data[0].indexOf("=") + 1);  String oauthVerifier = data[1].substring(data[1].indexOf("=") + 1);  OAuth oauth = getOAuthToken();  if (oauth != null && oauth.getToken() != null && oauth.getToken().getOauthTokenSecret() != null) { Flickr f = new Flickr(API_KEY, API_SEC, new REST());  OAuthInterface oauthApi = null;  if (f != null) {     oauthApi = f.getOAuthInterface();     return oauthApi.getAccessToken(oauthToken, oauthTokenSecret,verifier);}  } } Uploading Image to Flickr As we got the Access Token, we used that for creating new Flickr Object. The upload function present in the API requires three arguments - a file name, input stream and its metadata. The input stream is generated using the ContentResolver present in the Android. The code showing how we implemented uploading to Flickr is shown below. Flickr f = new Flickr(API_KEY, API_SEC, new REST()); RequestContext requestContext = RequestContext.getRequestContext(); OAuth auth = new OAuth(); auth.setToken(new OAuthToken(token, secret));…

Continue ReadingIntegration of Flickr in Phimpme Android

Using OpenCV for Native Image Processing in Phimpme Android

OpenCV is very widely used open-source image processing library. After the integration of OpenCV Android SDK in the Phimpme Android application, the image processing functions can be written in Java part or native part. Taking runtime of the functions into consideration we used native functions for image processing in the Phimpme application. We didn’t have the whole application written in native code, we just called the native functions on the Java OpenCV Mat object. Mat is short for the matrix in OpenCV. The image on which we perform image processing operations in the Phimpme Android application is stored as Mat object in OpenCV. Creating a Java OpenCV Mat object Mat object of OpenCV is same whether we use it in Java or C++. We have common OpenCV object in Phimpme for accessing from both Java part and native part of the application. We have a Java bitmap object on which we have to perform image processing operations using OpenCV. For doing that we need to create a Java Mat object and pass its address to native. Mat object of OpenCV can be created using the bitmap2Mat() function present in the OpenCV library. The implementation is shown below. Mat inputMat = new Mat(bitmap.getWidth(), bitmap.getHeight(), CvType.CV_8UC3); Utils.bitmapToMat(bitmap, inputMat); “bitmap” is the Java bitmap object which has the image to be processed. The third argument in the Mat function indicates that the Mat should be of type 8UC3 i.e. three color channels with 8-bit depth. With the second line above, the bitmap gets saved as the OpenCV Mat object. Passing Mat Object to Native We have the OpenCV Mat object in the memory. If we pass the whole object again to native, the same object gets copied from one memory location to another. In Phimpme application, instead of doing all that we can just get the memory location of the current OpenCV Mat object and pass it to native. As we have the address of the Mat, we can access it directly from native functions. Implementation of this is shown below. Native Function Definition: private static native void nativeApplyFilter(long inpAddr); Native Function call: nativeApplyFilter(inputMat.getNativeObjAddr()); Getting Native Mat Object to Java We can follow the similar steps for getting the Mat from the native part after processing. In the Java part of Phimpme, we created an OpenCV Mat object before we pass the inputMat OpenCV Mat object to native for processing. So we have inputMat and outputMat in the memory before we send them to native. We get the memory locations of both the Mat objects and pass those addresses to native part. After the processing is done, the data gets written to the same memory location and can be accessed in Java. The above functions can be modified and rewritten for this purpose as shown below Native Function Definition: private static native void nativeApplyFilter(long inpAddr, long outAddr ); Native Function call: nativeApplyFilter(inputMat.getNativeObjAddr(),outputMat.getNativeObjAddr()); inputMat.release(); if (outputMat !=null){   Bitmap outbit = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),bitmap.getConfig());   Utils.matToBitmap(outputMat,outbit);   outputMat.release();   return outbit; } Native operations on Mat using OpenCV The JNI…

Continue ReadingUsing OpenCV for Native Image Processing in Phimpme Android

Integrating OpenCV for Native Image Processing in Phimpme Android

OpenCV is an open source computer vision library written in C and C++. It has many pre-built image processing functions for all kinds of purposes. The filters and image enhancing functions which we implemented in Phimpme are not fully optimized. So we decided to integrate the Android SDK of OpenCV in the Phimpme Android application. For integrating OpenCV in the application, the files inside the OpenCV Android SDK have to be properly placed inside the project. The build and make files of the project have to be configured correspondingly. Here, in this post, I will mention how we fully integrated OpenCV Android SDK and configured the Phimpme project. Setting Up First, we downloaded the OpenCV-Android-SDK zip from the official release page here. Now we extracted the OpenCV-Android-SDK.zip file to navigated to SDK folder to see that there are three folders inside it. etc, Java and native. All the build files are present inside the native directory and are necessarily copied to our project. The java directory inside the SDK folder is an external gradle module which has to imported and added as a dependency to our project. So we copied all the files from jni directory of SDK folder inside OpenCV-Android-SDK to jni directory of the Phimpme project inside main. Similarly copied the 3rdparty directory into the main folder of the Phimpme project. Libs folder should also be copied into the main folder of the Phimpme but it has to be renamed to jniLibs. The project structure can be viewed in the below image. Adding Module to Project Now we headed to Android Studio and right clicked on the app in the project view, to add a new module. In that window select import Gradle project and browse to the java folder inside the SDK folder of OpenCV-Android-SDK as shown in figures. Now we opened the build.gradle file of app level (not project level) and added the following line as a dependency.  compile project(':openCVLibrary320') Now open the build.gradle inside the openCVLibrary320 external module and change the platform tools version and  SDK versions. Configuring Native Build Files We removed all files related to cmake be we use ndk-build to compile native code. After removing the cmake files, added following lines in the application.mk file APP_OPTIM := release APP_ABI := all APP_STL := gnustl_static APP_CPPFLAGS := -frtti -fexceptions APP_PLATFORM := android-25 Now we further added the following lines to the top of Android.mk file OPENCV_INSTALL_MODULES:=on OPENCV_CAMERA_MODULES:=on OPENCV_LIB_TYPE := STATIC include $(LOCAL_PATH)/OpenCV.mk The third line here is a flag indicating that the library has to be static and there is no need for external OpenCVLoader application. Finally, find this line OPENCV_LIBS_DIR:=$(OPENCV_THIS_DIR)/../libs/$(OPENCV_TARGET_ARCH_ABI) in OpenCV.mk file and replace it with OPENCV_LIBS_DIR:=$(OPENCV_THIS_DIR)/../jniLibs/$(OPENCV_TARGET_ARCH_ABI) And finally, add the following lines to java part. So that the OpenCV is initialized and linked to application static {   if (!OpenCVLoader.initDebug()) {       Log.e( TAG + " - Error", "Unable to load OpenCV");   } else {       System.loadLibrary("modulename_present_in_Android.mk");   } } With this, the integration of OpenCV and the configuration of the project is completed.…

Continue ReadingIntegrating OpenCV for Native Image Processing in Phimpme Android

Passing Java Bitmap Object to Native for Image Processing and Getting it back in Phimpme Android

To perform any image processing operations on an image, we must have an image object in native part like we have a Bitmap object in Java. We cannot just pass the image bitmap object directly as an argument to native function because ‘Bitmap’ is a java object and C/C++ cannot interpret it directly as an image. So, here I’ll discuss a method to send java Bitmap object to Native part for performing image processing operations on it, which we implemented in the image editor of Phimpme Android Image Application. C/C++ cannot interpret java bitmap object. So, we have to find the pixels of the java bitmap object and send them to native part to create a native bitmap over there. In Phimpme, we used a “struct” data structure in C to represent native bitmap. An image has three color channels red, green, blue. We consider alpha channel(transparency) for an argb8888 type image. But in Phimpme, we considered only three channels as it is enough to manipulate these channels to implement the edit functions, which we used in the Editor of Phimpme. We defined a struct, type-defined as NativeBitmap with attributes corresponding to all the three channels. We defined this in the nativebitmap.h header file in the jni/ folder of Phimpme so that we can include this header file in c files in which we needed to use a NativeBitmap struct. #ifndef NATIVEBITMAP #define NATIVEBITMAP #endif typedef struct { unsigned int width; unsigned int height; unsigned int redWidth; unsigned int redHeight; unsigned int greenWidth; unsigned int greenHeight; unsigned int blueWidth; unsigned int blueHeight; unsigned char* red; unsigned char* green; unsigned char* blue; } NativeBitmap; void deleteBitmap(NativeBitmap* bitmap); int initBitmapMemory(NativeBitmap* bitmap, int width, int height); As I explained in my previous post here on introduction to flow of native functions in Phimpme-Android, we defined the native functions with necessary arguments in Java part of Phimpme. We needed the image bitmap to be sent to the native part of the Phimpme application. So the argument in the function should have been java Bitmap object. But as mentioned earlier, the C code cannot interpret this java bitmap object directly as an image. So we created a function for getting pixels from a bitmap object in the Java class of Phimpme application. This function returns an array of unsigned integers which correspond to the pixels of a particular row of the image bitmap. The array of integers can be interpreted by C, so we can send this array of integers to native part and create a receiving native function to create NativeBitmap. We performed image processing operations like enhancing and applying filters on this NativeBitmap and after the processing is done, we sent the array of integers corresponding to a row of the image bitmap and constructed the Java Bitmap Object using those received arrays in java. The integers present in the array correspond to the color value of a particular pixel of the image bitmap. We used this color value to get red, green…

Continue ReadingPassing Java Bitmap Object to Native for Image Processing and Getting it back in Phimpme Android

Selecting Best persistent storage for Phimpme Android and how to use it

As we are progressing in our Phimpme Android app. I added account manager part which deals with connecting all other accounts to phimpme. Showing a list of connected accounts. We need a persistent storage to store all the details such as username, full name, profile image url, access token (to access API). I researched on various Object Relation mapping (ORMs) such as: DBFlow: https://github.com/Raizlabs/DBFlow GreenDAO: https://github.com/greenrobot/greenDAO SugarORM: http://satyan.github.io/sugar/ Requery: https://github.com/requery/requery and other NoSQL databases such as Realm Database : https://github.com/realm/realm-java. After reading a lot from some blogs on the benchmarking of these ORMs and database, I came to know that Realm database is quite better in terms of Speed of writing data and ease of use. Steps to integrate Realm Database: Installation of Realm database in android Following these steps https://realm.io/docs/java/latest/#installation quickly setup realm in android. Add classpath "io.realm:realm-gradle-plugin:3.3.2" in Project level build.gradle file and Add apply plugin: 'realm-android' in app level build.gradle, That’s it for using Realm Generating required Realm models Firstly, make sure what you need to store in your database. In case of phimpme, I first go through the account section and noted down what needs to be there.  Profile image URL, username, full name, account indicator image name. Below image illustrate this better. This is the Realm Model class I made in Kotlin to store name, username and access token for accessing API. open class AccountDatabase(       @PrimaryKey var name: String = "",       var username: String = "",       var token: String = "" ) : RealmObject() Writing data in database In Account manager, I create a add account option from where a dialog appear with a list of accounts. Currently, Twitter is working, when onSuccess function invoke in AccountPickerFragment I start a twitter session and store values in database. Writing data in database: // Begin realm transaction realm.beginTransaction(); // Creating Realm object for AccountDatabase Class account = realm.createObject(AccountDatabase.class,       accountsList[0]); account.setUsername(session.getUserName()); account.setToken(String.valueOf(session.getAuthToken())); realm.commitTransaction(); Begin and commit block in necessary. There is one more way of doing this is using execute function in Realm Use Separate Database Helper class for Database operations It’s good to use a separate class for all the Database operations needed in the project. I created a DatabaseHelper Class and added a function to query the result needed. Query the database public RealmResults<AccountDatabase> fetchAccountDetails(){   return realm.where(AccountDatabase.class).findAll(); } It give all of the results, stored in the database like below Problems I faced with annotation processor while using Kotlin and Realm together The Kotlin annotation processor not running due to the plugins wrong order. This issue https://github.com/realm/realm-java/pull/2568 helped me in solving that. I addded apply plugin: 'kotlin-kapt'. In app gradle file and shift apply plugin: 'realm-android' In below the order. Resources: Realm for Android (official): https://news.realm.io/news/realm-for-android/ Write in Realm Database: https://realm.io/docs/java/latest/#writes Queries in Realm Database: https://realm.io/docs/java/latest/#queries A detailed Blog post on Realm Database: https://medium.com/@Zhuinden/how-to-use-realm-for-android-like-a-champ-and-how-to-tell-if-youre-doing-it-wrong-ac4f66b7f149  

Continue ReadingSelecting Best persistent storage for Phimpme Android and how to use it

Porting Phimpme Android to Kotlin

As we are going ahead in Phimpme Project we are now on verge to start our account manager which deals with sharing images to many platforms right from the app. The account manager will take care of logging In the user. Saving it's important credentials such access token, username etc as required by the API. Google IO ‘17 just passed, and we seen tons of new features, APIs and development tools. One of the them is official support for Kotlin in Android Studio. As stated by the developers at the conference that one best way to work on Kotlin is add today in your project. Because it is compatible with Java, we can work together on both languages in the same project. It is not mandatory for you to shift your entire code to Kotlin to build a project. So starting with the account manager we decided to bring this to our code. It helps in reducing the boilerplate code for example in Phimpme, I created a model for Realm database. open class AccountDatabase(       @PrimaryKey var name: String = "",       var username: String = "",       var token: String = "" ) : RealmObject() That’s all I need to create a model class, no need to create getter and setter property This helps me to get details from getter methods and show on Account Manager Recycler View like below. Step 1 : Upgrade to Android Studio Preview 3.0 Android Studio Preview 3.0 comes up with all new features and Kotlin support. We all upgraded to that. It has a great Android Profiler with advance features for debugging and logcat is now moved separately. This step is not mandatory, you can work on older version of Android Studio as well. Step 2 : Configure Kotlin It’s easy in Android Studio Preview 3.0 to configure Kotlin. Go to Tools → Kotlin → Configure Kotlin in project. What in the configuration It added a classpath dependency in project level build.gradle file classpath"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" Added Kotlin plugin apply plugin: 'kotlin-android' Added kotlin std lib in app level build.gradle compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" Step 3: How to add Kotlin files Now your project is ready for Kotlin. In Android Studio Preview 3.0 you can create new Kotlin files from the file menu. Also by using Activity template you can select the source language as Java or Kotlin as per your preference. Step 4 : Work with Kotlin There are a lot new features in Kotlin to explore. Some of them are Null Safety : In Kotlin, the type system distinguishes between references that can hold null (nullable references) and those that can not (non-null references). For example, a regular variable of type String cannot hold null. var a: String = "abc" a = null // compilation error To allow nulls, we can declare a variable as nullable string, written String?: var b: String? = "abc" b = null // ok Val and Var are two keywords in Kotlin to declare variables. Val gives you read only variable which is same…

Continue ReadingPorting Phimpme Android to Kotlin

Adding Pinterest Integration in Phimpme Android

After establishing Facebook and Twitter share in Phimpme, our next goal was to integrate more social networking sites. Pinterest is an ideal website for us as it is widely used among various photography enthusiasts. Our goal was to share the image on the Pinterest website without the use of any other native android application. Note: First, we added Phimpme app in the developer section https://developers.pinterest.com/apps/. This step is crucial as it generates APP ID and it is necessary for authentication. Adding Pinterest option in Accounts Activity in Phimpme In accounts activity, we have added a list of accounts that we have integrated in a Recyclerview. Currently, we have integrated Facebook and Twitter. We need to add Pinterest App icon and Pinterest option in the Recyclerview. To add Pinterest app icon I downloaded the icon from iconfinder in SVG format. Using SVG format allows icon to resize itself according to the screen sizes of the users. We saved the icon file name as ic_pinterest_black.xml Pinterest icon SVG: <vector android:height="24dp" android:viewportHeight="32.0"   android:viewportWidth="32.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">   <path android:fillColor="#231F20" android:pathData="M16,0.27C7.16,0.27 0,7.43 0,16.27c0,6.55 3.94,12.18 9.58,14.65c-0.05,-1.12 -0.01,-2.46 0.28,-3.67c0.31,-1.3 2.06,-8.72 2.06,-8.72s-0.51,-1.02 -0.51,-2.53c0,-2.37 1.37,-4.14 3.08,-4.14c1.46,0 2.16,1.09 2.16,2.4c0,1.46 -0.93,3.65 -1.41,5.68c-0.4,1.7 0.85,3.08 2.53,3.08c3.03,0 5.07,-3.89 5.07,-8.51c0,-3.51 -2.36,-6.13 -6.66,-6.13c-4.85,0 -7.88,3.62 -7.88,7.66c0,1.39 0.41,2.38 1.05,3.14c0.29,0.35 0.34,0.49 0.23,0.89C9.51,20.37 9.33,21.08 9.26,21.36c-0.11,0.4 -0.44,0.55 -0.8,0.4c-2.23,-0.91 -3.28,-3.36 -3.28,-6.11c0,-4.55 3.83,-9.99 11.44,-9.99c6.11,0 10.13,4.42 10.13,9.16c0,6.28 -3.49,10.97 -8.63,10.97c-1.73,0 -3.35,-0.93 -3.91,-1.99c0,0 -0.93,3.69 -1.13,4.4c-0.34,1.23 -1,2.46 -1.61,3.43C12.9,32.04 14.43,32.27 16,32.27c8.84,0 16,-7.16 16,-16S24.84,0.27 16,0.27z"/> </vector> We made an array of all the accounts list in the Accounts Activity. I added Pinterest in the array list. This array list is inflated in the Accounts Activity. public static String[] accountName = {"Facebook", "Twitter", "Drupal", "NextCloud", "Wordpress", "Pinterest"}; To add the icon the recyclerview, we have to get the resource id from the drawable folder and then set is in the recyclerview. We have done it dynamically so that we don’t have to fetch the id of every accounts icons. Integer id = getContext().getResources().getIdentifier(context.getString(R.string.ic_) +               (accountName[position].toLowerCase()) + "_black"       , context.getString(R.string.drawable)       , getContext().getPackageName()); holder.accountAvatar.setImageResource(id); Importing Pinterest SDK in Phimpme project To import the Pinterest SDK in the project, please download the Pinterest SDK from the link: http://assets.pinterest.com/sdk/android-pdk.tar. Go to File->import new module->Import Gradle Project. Build the project, if there is any error resolve the error and then build Gradle again. compile project(‘:pdk’) Add APP ID in the manifest file of the project We need to add the APP ID that was generated while making the app the Pinterest in the manifest folder of the Phimpme Android. In the case of Phimpme, we have added the APP ID in the intent filter in the Accounts Activity. This is necessary for the interaction of the Phimpme with the Pinterest website. <activity   android:name=".accounts.AccountActivity"   android:screenOrientation="portrait"   android:theme="@style/Theme.AppCompat.NoActionBar">   <intent-filter>       <action android:name="android.intent.action.VIEW" />       <category android:name="android.intent.category.DEFAULT" />       <category android:name="android.intent.category.BROWSABLE" />       <data android:scheme="pdk4910600717739247160" />   </intent-filter> </activity> Establishing Pinterest authentication in Accounts Activity First, we need to import all the necessary class from the Pinterest SDK. These class includes callback functions, PDKClient which manages the interface between the Phimpme app and the Pinterest website. We need to pass the APP ID in the PDKClient…

Continue ReadingAdding Pinterest Integration in Phimpme Android