Skip to content

Commit

Permalink
Added url shortener api
Browse files Browse the repository at this point in the history
  • Loading branch information
sahildave committed Oct 22, 2014
1 parent 7b85fdc commit 950349d
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 19 deletions.
Binary file added libs/google-api-client-1.19.0.jar
Binary file not shown.
Binary file added libs/google-api-client-android-1.19.0.jar
Binary file not shown.
Binary file not shown.
Binary file added libs/google-http-client-1.19.0.jar
Binary file not shown.
Binary file added libs/google-http-client-android-1.19.0.jar
Binary file not shown.
Binary file added res/drawable/ic_title_white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 6 additions & 4 deletions res/layout/fragment_welcome_screen_first_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
android:layout_gravity="center_horizontal"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:gravity="center"
android:fontFamily="sans-serif-condensed"
android:text="Because Reading News\n Should Not Be Boring"
android:text="Instant News\nOn Your Fingertips"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"
android:textStyle="bold"/>
android:textStyle="normal"/>

<View
android:layout_width="240dp"
Expand All @@ -44,16 +45,17 @@
<ImageView
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:src="@drawable/ic_launcher"/>
android:src="@drawable/ic_title_white"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="@string/app_name"
android:visibility="gone"
android:fontFamily="sans-serif-condensed"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white">
Expand Down
2 changes: 1 addition & 1 deletion res/menu/detail_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/Share"
android:id="@+id/action_share"
android:orderInCategory="99"
android:title="Share"
android:icon="@android:drawable/ic_menu_share"
Expand Down
Binary file added screens/welcome_screen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 19 additions & 4 deletions src/in/sahildave/gazetti/bookmarks/BookmarkDetailFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import in.sahildave.gazetti.R;
import in.sahildave.gazetti.bookmarks.sqlite.BookmarkDataSource;
import in.sahildave.gazetti.bookmarks.sqlite.BookmarkModel;
import in.sahildave.gazetti.util.ShareButtonListener;

public class BookmarkDetailFragment extends Fragment {
private static final String TAG = BookmarkDetailFragment.class.getName();
Expand All @@ -42,6 +43,7 @@ public class BookmarkDetailFragment extends Fragment {
private Button mReadItLater;
private boolean bookmarked = true;
private BookmarkDataSource dataSource;
private Button mViewInBrowser;

static interface BookmarkLoadArticleCallback {
void onPreExecute(View rootView);
Expand Down Expand Up @@ -106,7 +108,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
mReadItLater = (Button) rootView.findViewById(R.id.read_it_later);
mReadItLater.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_bookmark_done, 0, 0, 0);
Button mShareButton = (Button) rootView.findViewById(R.id.shareContent);
Button mViewInBrowser = (Button) rootView.findViewById(R.id.viewInBrowser);
mViewInBrowser = (Button) rootView.findViewById(R.id.viewInBrowser);

TextView categoryName = (TextView) rootView.findViewById(R.id.category);
categoryName.setText(catNameString);
Expand All @@ -125,6 +127,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
mNewspaperTile.setOnClickListener(webViewCalled);
mViewInBrowser.setOnClickListener(webViewCalled);
mReadItLater.setOnClickListener(readItLater);
mShareButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
new ShareButtonListener(getActivity(), mArticleURL, mArticleHeadline);
}
});

mScrollToReadLayout = (LinearLayout) rootView.findViewById(R.id.scrollToReadLayout);
mScrollToReadLayout.setVisibility(View.INVISIBLE);
Expand Down Expand Up @@ -162,10 +170,17 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(getActivity(), "Clicked "+item.getTitle(), Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}

switch (item.getItemId()){
case R.id.action_share:
new ShareButtonListener(getActivity(), mArticleURL, mArticleHeadline);
return true;
case R.id.action_view_in_browser:
mViewInBrowser.performClick();
return true;
}
return false;
}

@Override
public void onDetach() {
Expand Down
8 changes: 0 additions & 8 deletions src/in/sahildave/gazetti/homescreen/HomeScreenFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,8 @@ public void onResume() {
SharedPreferences sharedPref = getActivity().getSharedPreferences("CellList", Context.MODE_PRIVATE);
int newfeedVersion = sharedPref.getInt("feedVersion", 0);

Log.d(TAG, "HomeScreenFragment in onResume: firstRun - " + firstRun
+ ", newfeedVerision - " + newfeedVersion
+ ", feedVersion - " + feedVersion);
if ((newfeedVersion > feedVersion)) {

Log.d(TAG, "RELOADING - " + cellList.size());
feedVersion = newfeedVersion;
cellList.clear();
cellList = CellListUtil.getCellListFromSharedPrefs();
Expand All @@ -104,9 +100,6 @@ public void onResume() {
animAdapterMultiple.setAbsListView(gridview);

gridview.setAdapter(animAdapterMultiple);

Log.d(TAG, "LOADED - " + cellList.size());

}

}
Expand All @@ -121,7 +114,6 @@ public void onCreate(Bundle savedInstanceState) {
feedVersion = sharedPref.getInt("feedVersion", 0);

firstRun = true;
Log.d(TAG, "FEEDVERSION onCreate - " + feedVersion);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import in.sahildave.gazetti.news_activities.fetch.hindu;
import in.sahildave.gazetti.news_activities.fetch.indianExpress;
import in.sahildave.gazetti.news_activities.fetch.toi;
import in.sahildave.gazetti.util.ShareButtonListener;

public class WebsiteDetailFragment extends Fragment {
private static final String TAG = "DFRAGMENT";
Expand All @@ -48,6 +49,7 @@ public class WebsiteDetailFragment extends Fragment {
private Button mReadItLater;
private boolean bookmarked = false;
private BookmarkDataSource dataSource;
private Button mViewInBrowser;

static interface LoadArticleCallback {
void onPreExecute(View rootView);
Expand Down Expand Up @@ -106,7 +108,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
ImageButton mNewspaperTile = (ImageButton) rootView.findViewById(R.id.newspaperTile);
mReadItLater = (Button) rootView.findViewById(R.id.read_it_later);
Button mShareButton = (Button) rootView.findViewById(R.id.shareContent);
Button mViewInBrowser = (Button) rootView.findViewById(R.id.viewInBrowser);
mViewInBrowser = (Button) rootView.findViewById(R.id.viewInBrowser);

TextView categoryName = (TextView) rootView.findViewById(R.id.category);
categoryName.setText(catNameString);
Expand All @@ -125,6 +127,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
mNewspaperTile.setOnClickListener(webViewCalled);
mViewInBrowser.setOnClickListener(webViewCalled);
mReadItLater.setOnClickListener(readItLater);
mShareButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
new ShareButtonListener(getActivity(), mArticleURL, mArticleHeadline);
}
});

mScrollToReadLayout = (LinearLayout) rootView.findViewById(R.id.scrollToReadLayout);
mScrollToReadLayout.setVisibility(View.INVISIBLE);
Expand Down Expand Up @@ -172,11 +180,19 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(getActivity(), "Clicked "+item.getTitle(), Toast.LENGTH_SHORT).show();
switch (item.getItemId()){
case R.id.action_share:
new ShareButtonListener(getActivity(), mArticleURL, mArticleHeadline);
return true;
case R.id.action_view_in_browser:
mViewInBrowser.performClick();
return true;
}
return super.onOptionsItemSelected(item);
}



@Override
public void onDetach() {
Log.d(TAG, "DetailFragment onDetach");
Expand Down
86 changes: 86 additions & 0 deletions src/in/sahildave/gazetti/util/ShareButtonListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package in.sahildave.gazetti.util;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import com.crashlytics.android.Crashlytics;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.services.urlshortener.Urlshortener;
import com.google.api.services.urlshortener.model.Url;

import java.io.IOException;

/**
* Created by sahil on 22/10/14.
*/
public class ShareButtonListener {
private static final int HEADLINE_LENGTH = 120;
private static final int MESSAGE_LENGTH = 137;

Context context;
String mArticleURL;
String mArticleHeadLine;
public ShareButtonListener(Context context, String mArticleURL, String mArticleHeadline) {
Log.d(ShareButtonListener.class.getName(), "Sharing...");
this.context = context;
this.mArticleURL = mArticleURL;
this.mArticleHeadLine = mArticleHeadline;

mArticleHeadLine = mArticleHeadLine.length() > HEADLINE_LENGTH ?
mArticleHeadLine.substring(0, HEADLINE_LENGTH) :
mArticleHeadLine;

shorten(mArticleURL);

}

private void shorten(String longUrl){

new AsyncTask<String, Void, String>(){

@Override
protected String doInBackground(String... params) {
String longUrl = params[0];

Urlshortener.Builder builder = new Urlshortener.Builder (AndroidHttp.newCompatibleTransport(), AndroidJsonFactory.getDefaultInstance(), null);
Urlshortener urlshortener = builder.build();

Url url = new Url();
url.setLongUrl(longUrl);
try {
url = urlshortener.url().insert(url).execute();
return url.getId();
} catch (IOException e) {
return null;
}
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);

try {
mArticleURL = s;

String intentString = mArticleHeadLine + " - "+mArticleURL;
intentString = intentString.length() > MESSAGE_LENGTH ?
intentString.substring(0, MESSAGE_LENGTH)+"..." :
intentString;

Log.d(ShareButtonListener.class.getName(), "Sharing content - " + intentString);

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, intentString);
sendIntent.setType("text/");
context.startActivity(Intent.createChooser(sendIntent, "Share with"));
} catch (Exception e) {
Crashlytics.logException(e);
e.printStackTrace();
}
}
}.execute(longUrl);
}
}

0 comments on commit 950349d

Please sign in to comment.