Created
July 1, 2017 14:20
-
-
Save m7mdra/88dde4d0f25ff212eb08ca47f22e9404 to your computer and use it in GitHub Desktop.
This file contains 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
package com.reqabaweb.healthcontrol.ui.activity; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.print.PrintAttributes; | |
import android.print.PrintDocumentAdapter; | |
import android.print.PrintJob; | |
import android.print.PrintManager; | |
import android.support.v7.widget.Toolbar; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
import android.widget.RelativeLayout; | |
import com.reqabaweb.healthcontrol.Const; | |
import com.reqabaweb.healthcontrol.R; | |
import com.reqabaweb.healthcontrol.model.Visit; | |
import com.reqabaweb.healthcontrol.ui.activity.tabs.VisitActivity; | |
import com.reqabaweb.healthcontrol.util.SessionHandler; | |
import com.reqabaweb.healthcontrol.util.ToolbarUtils; | |
import java.io.IOException; | |
import java.util.concurrent.Executors; | |
import okhttp3.Call; | |
import okhttp3.Callback; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import static com.reqabaweb.healthcontrol.Const.Properties.PARAM_X_AUTH_TOKEN; | |
public class PrintActivity extends BaseActivity implements Callback { | |
private Toolbar toolbar; | |
private WebView mWebView; | |
private boolean isPageLoaded = false; | |
private boolean isDataLoaded = false; | |
private Visit visit; | |
private Intent i; | |
private RelativeLayout progressLayout; | |
private DownloadInvoiceTask downloadInvoceTask; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_print); | |
i = getIntent(); | |
downloadInvoceTask=new DownloadInvoiceTask(); | |
visit = i.getParcelableExtra(VisitActivity.VISIT); | |
progressLayout = (RelativeLayout) findViewById(R.id.progress_layout); | |
toolbar = (Toolbar) findViewById(R.id.toolbar); | |
mWebView = (WebView) findViewById(R.id.webview); | |
setupToolBar(); | |
mWebView.setWebViewClient(new WebViewClient() { | |
public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
return false; | |
} | |
@Override | |
public void onPageFinished(WebView view, String url) { | |
isPageLoaded = true; | |
} | |
}); | |
getOkHttpClient() | |
.newBuilder() | |
.addInterceptor(new SessionHandler(this)) | |
.build() | |
.newCall(new Request.Builder() | |
.url(getBaseUrl() + Const.VISITS_URL + "/" + visit.getVisitId() + "/invoice") | |
.header(PARAM_X_AUTH_TOKEN, getToken()).build()) | |
.enqueue(this); | |
} | |
private void setupToolBar() { | |
setSupportActionBar(toolbar); | |
if (getSupportActionBar() != null) | |
getSupportActionBar().setTitle("طباعة تقرير"); | |
ToolbarUtils.changeToolbar(toolbar); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.menu_visit_activity, menu); | |
MenuItem item=menu.findItem(R.id.action_delete_visit); | |
item.setVisible(false); | |
return super.onCreateOptionsMenu(menu); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
if (item.getItemId() == R.id.action_print) { | |
if (isDataLoaded) | |
if (isPageLoaded) | |
createWebPrintJob(mWebView); | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
private void createWebPrintJob(WebView webView) { | |
PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE); | |
// Get a print adapter instance | |
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter(); | |
// Create a print job with name and adapter instance | |
String jobName = getString(R.string.app_name) + " Document"; | |
PrintJob printJob = printManager.print(jobName, printAdapter, | |
new PrintAttributes.Builder().build()); | |
Log.d(TAG, "createWebPrintJob: " + printJob.getInfo().toString()); | |
} | |
@Override | |
public void onFailure(Call call, IOException e) { | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
progressLayout.setVisibility(View.GONE); | |
} | |
}); | |
} | |
@Override | |
public void onResponse(Call call, final Response response) throws IOException { | |
if (response.isSuccessful()) { | |
isDataLoaded = true; | |
downloadInvoceTask.executeOnExecutor(Executors.newSingleThreadExecutor(),response); | |
} | |
} | |
public class DownloadInvoiceTask extends AsyncTask<Response, Void, String> { | |
@Override | |
protected String doInBackground(Response... responses) { | |
try { | |
return responses[0].body().string(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(String s) { | |
super.onPostExecute(s); | |
Log.d(TAG, "onPostExecute: " + s); | |
progressLayout.setVisibility(View.GONE); | |
mWebView.setVisibility(View.VISIBLE); | |
mWebView.loadDataWithBaseURL(null, s, "text/HTML", "UTF-8", null); | |
} | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
if (downloadInvoceTask!=null ) | |
downloadInvoceTask.cancel(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment