Last active
March 22, 2017 15:50
-
-
Save haideralipunjabi/f677e14005d2aa127ab338c9d9647899 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void PostToGroup(final Activity activity, final byte[] image, final String caption, final PostDelegate postDelegate) | |
{ | |
String post_request = "/" + "1631546380475418"; | |
if(image != null) post_request += "/photos"; | |
else post_request += "/feed"; | |
Bundle params = new Bundle(); | |
params.putString("message", caption); | |
if(image != null) params.putByteArray("source", image); | |
FacebookSdk.setOnProgressThreshold(1l); | |
GraphRequest.OnProgressCallback gCallBack = new GraphRequest.OnProgressCallback() { | |
@Override | |
public void onProgress(long current, long max) { | |
Log.d("ONProgress", "onProgress() called with: current = [" + current + "], max = [" + max + "]"); | |
int progress = Math.round((current * 100) / max); | |
Log.d("Progress: " ,String.valueOf(progress)); | |
} | |
@Override | |
public void onCompleted(GraphResponse response) { | |
JSONObject jsonObject = response.getJSONObject(); | |
if(jsonObject != null && jsonObject.has("id")) { | |
postDelegate.onSuccess(jsonObject.optString("id")); | |
} | |
else | |
{ | |
postDelegate.onError(); | |
} | |
} | |
}; | |
final GraphRequest graphRequest = new GraphRequest( | |
AccessToken.getCurrentAccessToken(), | |
post_request, | |
params, | |
HttpMethod.POST, | |
gCallBack); | |
graphRequest.executeAsync(); | |
} | |
public void PostButtonClick(View v){ | |
//KPCExplorer.internet is a boolean to check for internet connection | |
if (KPCExplorer.internet) { | |
if (!facebookManager.hasPublishPermission()) { | |
getPublishPermission(((PostPhoto) mContext), new FacebookManager.PostDelegate() { | |
@Override | |
public void onSuccess(LoginResult loginResult) { | |
//photoBitmap has the photo to be uploaded | |
Toasty.success(mContext, "Login Successful", Toast.LENGTH_SHORT, true).show(); | |
if(photoBitmap !=null){ | |
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | |
photoBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); | |
byte[] byteArray = stream.toByteArray(); | |
PostPhotoService.bitmapArray = byteArray; | |
PostPhotoService.caption = caption.getText().toString(); | |
Intent intent = new Intent(getBaseContext(), PostPhotoService.class); | |
startService(intent); | |
}else{ | |
PostToGroup((PostPhoto) mContext, null, caption.getText().toString(), new PostDelegate() { | |
@Override | |
public void onSuccess(String id) { | |
Toasty.success(mContext, "Your post has been posted", Toast.LENGTH_SHORT,true).show(); | |
} | |
@Override | |
public void onError() { | |
Toasty.error(mContext, "An error occurred while posting", Toast.LENGTH_SHORT,true).show(); | |
} | |
}); | |
} | |
} | |
@Override | |
public void onCancel() { | |
Toasty.info(mContext, "Login Cancelled", Toast.LENGTH_SHORT, true).show(); | |
} | |
@Override | |
public void onError(FacebookException error) { | |
Toasty.error(mContext, "Login Error", Toast.LENGTH_SHORT, true).show(); | |
} | |
}); | |
} | |
else if (facebookManager.hasPublishPermission()) { | |
if(photoBitmap != null){ | |
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | |
photoBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); | |
byte[] byteArray = stream.toByteArray(); | |
PostPhotoService.bitmapArray = byteArray; | |
PostPhotoService.caption = caption.getText().toString(); | |
Intent intent = new Intent(getBaseContext(), PostPhotoService.class); | |
startService(intent);} | |
else{ | |
PostToGroup((PostPhoto) mContext, null, caption.getText().toString(), new PostDelegate() { | |
@Override | |
public void onSuccess(String id) { | |
Toasty.success(mContext, "Your post has been posted", Toast.LENGTH_SHORT,true).show(); | |
} | |
@Override | |
public void onError() { | |
Toasty.error(mContext, "An error occurred while posting", Toast.LENGTH_SHORT,true).show(); | |
} | |
}); | |
} | |
} | |
} | |
else | |
{ | |
Toasty.info(this, "Please connect to the internet", Toast.LENGTH_SHORT, true).show(); | |
} | |
} | |
public static class PostPhotoService extends IntentService { | |
public static byte[] bitmapArray; | |
public static String caption; | |
private boolean failed = true; | |
public PostPhotoService(){ | |
super("PostPhotoService"); | |
} | |
@Nullable | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} | |
@Override | |
protected void onHandleIntent(Intent intent) { | |
Intent notificationIntent = new Intent(this, MainActivity.class); | |
PendingIntent contentIntent = PendingIntent.getActivity(this, 1, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
notificationManager.Update("Uploading Photo", "Your photo will be uploaded in a while", true); | |
final Snackbar snackbar = Snackbar.make(post_photo_layout, "Started photo upload", Snackbar.LENGTH_SHORT); | |
((TextView)snackbar.getView().findViewById(android.support.design.R.id.snackbar_text)).setTextColor(Color.GREEN); | |
snackbar.show(); | |
PostToGroup((PostPhoto) mContext, bitmapArray, this.caption, new PostDelegate() { | |
@Override | |
public void onSuccess(String id) { | |
Toasty.success(mContext, "Your photo has been posted", Toast.LENGTH_SHORT,true).show(); | |
notificationManager.Update("Photo uploaded", "Your photo has been uploaded",false); | |
failed = false; | |
stopSelf(); | |
} | |
@Override | |
public void onError() { | |
Toasty.error(mContext, "An error occurred while uploading your photo", Toast.LENGTH_SHORT,true).show(); | |
notificationManager.Update("Uploading Photo Failed!", "Your photo wasn't uploaded",false); | |
failed = true; | |
stopSelf(); | |
} | |
}); | |
return START_NOT_STICKY; | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
if(failed) | |
{ | |
notificationManager.Update("Uploading Photo Failed!", "Your photo wasn't uploaded",false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment